<!DOCTYPE html>
<html lang="zh-cn">
<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <title>Python3常用标准库 - vzvixb</title>
  <meta name="renderer" content="webkit" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>

<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />

<meta name="theme-color" content="#f8f5ec" />
<meta name="msapplication-navbutton-color" content="#f8f5ec">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="#f8f5ec">


<meta name="author" content="even" /><meta name="description" content="Python3 常用标准库 python3 内置函数 os模块 os对象 os.path对象 (可以使用pathlib替换) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27" /><meta name="keywords" content="Hugo, theme, even" />






<meta name="generator" content="Hugo 0.90.1 with theme even" />


<link rel="canonical" href="https://zhouxiaoxin.gitee.io/post/python/python3%E5%B8%B8%E7%94%A8%E6%A0%87%E5%87%86%E5%BA%93/" />
<link rel="apple-touch-icon" sizes="180x180" href="/apple-touch-icon.png">
<link rel="icon" type="image/png" sizes="32x32" href="/favicon-32x32.png">
<link rel="icon" type="image/png" sizes="16x16" href="/favicon-16x16.png">
<link rel="manifest" href="/manifest.json">
<link rel="mask-icon" href="/safari-pinned-tab.svg" color="#5bbad5">

<script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>

<link href="/sass/main.min.32d4dc642fec98c34c80bebb9c784c50771712b4a8a25d9f4dd9cce3534b426e.css" rel="stylesheet">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@3.1.20/dist/jquery.fancybox.min.css" integrity="sha256-7TyXnr2YU040zfSP+rEcz29ggW4j56/ujTPwjMzyqFY=" crossorigin="anonymous">


<meta property="og:title" content="Python3常用标准库" />
<meta property="og:description" content="Python3 常用标准库 python3 内置函数 os模块 os对象 os.path对象 (可以使用pathlib替换) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27" />
<meta property="og:type" content="article" />
<meta property="og:url" content="https://zhouxiaoxin.gitee.io/post/python/python3%E5%B8%B8%E7%94%A8%E6%A0%87%E5%87%86%E5%BA%93/" /><meta property="article:section" content="post" />
<meta property="article:published_time" content="2018-12-10T20:46:21+08:00" />
<meta property="article:modified_time" content="2018-12-10T20:46:21+08:00" />

<meta itemprop="name" content="Python3常用标准库">
<meta itemprop="description" content="Python3 常用标准库 python3 内置函数 os模块 os对象 os.path对象 (可以使用pathlib替换) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27"><meta itemprop="datePublished" content="2018-12-10T20:46:21+08:00" />
<meta itemprop="dateModified" content="2018-12-10T20:46:21+08:00" />
<meta itemprop="wordCount" content="6777">
<meta itemprop="keywords" content="Python," /><meta name="twitter:card" content="summary"/>
<meta name="twitter:title" content="Python3常用标准库"/>
<meta name="twitter:description" content="Python3 常用标准库 python3 内置函数 os模块 os对象 os.path对象 (可以使用pathlib替换) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27"/>

<!--[if lte IE 9]>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/classlist/1.1.20170427/classList.min.js"></script>
<![endif]-->

<!--[if lt IE 9]>
  <script src="https://cdn.jsdelivr.net/npm/html5shiv@3.7.3/dist/html5shiv.min.js"></script>
  <script src="https://cdn.jsdelivr.net/npm/respond.js@1.4.2/dest/respond.min.js"></script>
<![endif]-->

</head>
<body>
  <div id="mobile-navbar" class="mobile-navbar">
  <div class="mobile-header-logo">
    <a href="/" class="logo">Even</a>
  </div>
  <div class="mobile-navbar-icon">
    <span></span>
    <span></span>
    <span></span>
  </div>
</div>
<nav id="mobile-menu" class="mobile-menu slideout-menu">
  <ul class="mobile-menu-list">
    <a href="/">
        <li class="mobile-menu-item">Home</li>
      </a><a href="/post/">
        <li class="mobile-menu-item">Archs</li>
      </a><a href="/tags/">
        <li class="mobile-menu-item">Tags</li>
      </a><a href="/categories/">
        <li class="mobile-menu-item">Cates</li>
      </a><a href="/about/">
        <li class="mobile-menu-item">About</li>
      </a><a href="/pages/runoob/">
        <li class="mobile-menu-item">runoob</li>
      </a><a href="/pages/98wubi/">
        <li class="mobile-menu-item">98wubi</li>
      </a>
  </ul>
</nav>
  <div class="container" id="mobile-panel">
    <header id="header" class="header">
        <div class="logo-wrapper">
  <a href="/" class="logo">Even</a>
</div>

<nav class="site-navbar">
  <ul id="menu" class="menu">
    <li class="menu-item">
        <a class="menu-item-link" href="/">Home</a>
      </li><li class="menu-item">
        <a class="menu-item-link" href="/post/">Archs</a>
      </li><li class="menu-item">
        <a class="menu-item-link" href="/tags/">Tags</a>
      </li><li class="menu-item">
        <a class="menu-item-link" href="/categories/">Cates</a>
      </li><li class="menu-item">
        <a class="menu-item-link" href="/about/">About</a>
      </li><li class="menu-item">
        <a class="menu-item-link" href="/pages/runoob/">runoob</a>
      </li><li class="menu-item">
        <a class="menu-item-link" href="/pages/98wubi/">98wubi</a>
      </li>
  </ul>
</nav>
    </header>

    <main id="main" class="main">
      <div class="content-wrapper">
        <div id="content" class="content">
          <article class="post">
    
    <header class="post-header">
      <h1 class="post-title">Python3常用标准库</h1>

      <div class="post-meta">
        <span class="post-time"> 2018-12-10 </span>
        <div class="post-category">
            <a href="/categories/python/"> Python </a>
            </div>
          <span class="more-meta"> 约 6777 字 </span>
          <span class="more-meta"> 预计阅读 14 分钟 </span>
        <span id="busuanzi_container_page_pv" class="more-meta"> <span id="busuanzi_value_page_pv"><img src="/img/spinner.svg" alt="spinner.svg"/></span> 次阅读 </span>
      </div>
    </header>

    <div class="post-toc" id="post-toc">
  <h2 class="post-toc-title">文章目录</h2>
  <div class="post-toc-content">
    <nav id="TableOfContents">
  <ul>
    <li><a href="#python3-常用标准库">Python3 常用标准库</a>
      <ul>
        <li><a href="#os模块">os模块</a></li>
        <li><a href="#shutil">shutil</a></li>
        <li><a href="#urllib库">urllib库</a></li>
        <li><a href="#random模块">random模块</a></li>
        <li><a href="#time和datetime模块">time和datetime模块</a></li>
        <li><a href="#json模块">json模块</a></li>
        <li><a href="#copy模块">copy模块</a></li>
        <li><a href="#operator操作符">operator操作符</a></li>
        <li><a href="#collections模块">collections模块</a></li>
      </ul>
    </li>
  </ul>
</nav>
  </div>
</div>
    <div class="post-content">
      <h2 id="python3-常用标准库">Python3 常用标准库</h2>
<p><a href="https://www.cnblogs.com/wang-yc/p/5635765.html">python3 内置函数</a></p>
<h3 id="os模块">os模块</h3>
<ul>
<li>os对象</li>
<li>os.path对象 (<strong><a href="https://xin053.github.io/2016/07/03/pathlib%E8%B7%AF%E5%BE%84%E5%BA%93%E4%BD%BF%E7%94%A8%E8%AF%A6%E8%A7%A3/">可以使用pathlib替换</a></strong>)</li>
</ul>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span><span class="lnt">14
</span><span class="lnt">15
</span><span class="lnt">16
</span><span class="lnt">17
</span><span class="lnt">18
</span><span class="lnt">19
</span><span class="lnt">20
</span><span class="lnt">21
</span><span class="lnt">22
</span><span class="lnt">23
</span><span class="lnt">24
</span><span class="lnt">25
</span><span class="lnt">26
</span><span class="lnt">27
</span><span class="lnt">28
</span><span class="lnt">29
</span><span class="lnt">30
</span><span class="lnt">31
</span><span class="lnt">32
</span><span class="lnt">33
</span><span class="lnt">34
</span><span class="lnt">35
</span><span class="lnt">36
</span><span class="lnt">37
</span><span class="lnt">38
</span><span class="lnt">39
</span><span class="lnt">40
</span><span class="lnt">41
</span><span class="lnt">42
</span><span class="lnt">43
</span><span class="lnt">44
</span><span class="lnt">45
</span><span class="lnt">46
</span><span class="lnt">47
</span><span class="lnt">48
</span><span class="lnt">49
</span><span class="lnt">50
</span><span class="lnt">51
</span><span class="lnt">52
</span><span class="lnt">53
</span><span class="lnt">54
</span><span class="lnt">55
</span><span class="lnt">56
</span><span class="lnt">57
</span><span class="lnt">58
</span><span class="lnt">59
</span><span class="lnt">60
</span><span class="lnt">61
</span><span class="lnt">62
</span><span class="lnt">63
</span><span class="lnt">64
</span><span class="lnt">65
</span><span class="lnt">66
</span><span class="lnt">67
</span><span class="lnt">68
</span><span class="lnt">69
</span><span class="lnt">70
</span><span class="lnt">71
</span><span class="lnt">72
</span><span class="lnt">73
</span><span class="lnt">74
</span><span class="lnt">75
</span><span class="lnt">76
</span><span class="lnt">77
</span><span class="lnt">78
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-python" data-lang="python">
<span class="c1"># -*- coding:utf-8 -*-</span>
<span class="c1"># Author: Evan Mi</span>
<span class="kn">import</span> <span class="nn">os</span>
<span class="c1"># 获取当前工作目录，即当前python脚本工作的目录路径</span>
<span class="n">os</span><span class="o">.</span><span class="n">getcwd</span><span class="p">()</span>
<span class="c1"># 切换当前路径</span>
<span class="n">os</span><span class="o">.</span><span class="n">chdir</span><span class="p">(</span><span class="s1">&#39;c:/users&#39;</span><span class="p">)</span>
<span class="n">os</span><span class="o">.</span><span class="n">chdir</span><span class="p">(</span><span class="sa">r</span><span class="s1">&#39;c:\users&#39;</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="n">os</span><span class="o">.</span><span class="n">getcwd</span><span class="p">())</span>  <span class="c1"># 验证当前路径是否切换成功</span>
<span class="c1"># 返回当前目录的字符串名 .</span>
<span class="n">os</span><span class="o">.</span><span class="n">curdir</span>
<span class="c1"># 返回当前目录的父目录的字符串名 ..</span>
<span class="n">os</span><span class="o">.</span><span class="n">pardir</span>
<span class="c1"># 创建单层文件目录，如果目录存在，则抛出异常</span>
<span class="n">os</span><span class="o">.</span><span class="n">mkdir</span><span class="p">(</span><span class="sa">r</span><span class="s1">&#39;e:\a\b\c\d&#39;</span><span class="p">)</span>
<span class="c1"># 递归创建各级目录，如果目录存在，则抛出异常</span>
<span class="n">os</span><span class="o">.</span><span class="n">makedirs</span><span class="p">(</span><span class="sa">r</span><span class="s1">&#39;e:\a\b\c\d&#39;</span><span class="p">)</span>
<span class="c1"># 删除文件</span>
<span class="n">os</span><span class="o">.</span><span class="n">rmove</span><span class="p">(</span><span class="n">path</span><span class="p">)</span>
<span class="c1"># 删除单层目录，如果目录非空，则抛出异常</span>
<span class="n">os</span><span class="o">.</span><span class="n">rmdir</span><span class="p">(</span><span class="sa">r</span><span class="s1">&#39;e:\a\b&#39;</span><span class="p">)</span>
<span class="c1"># 递归删除各级空目录</span>
<span class="n">os</span><span class="o">.</span><span class="n">removedirs</span><span class="p">(</span><span class="sa">r</span><span class="s1">&#39;e:\a\b\c\d&#39;</span><span class="p">)</span>
<span class="c1"># 创建一级目录，不能递归创建</span>
<span class="n">os</span><span class="o">.</span><span class="n">mkdir</span><span class="p">(</span><span class="sa">r</span><span class="s1">&#39;e:/test_python&#39;</span><span class="p">)</span>
<span class="c1"># 删除一级目录，切该目录不能为空</span>
<span class="n">os</span><span class="o">.</span><span class="n">rmdir</span><span class="p">(</span><span class="sa">r</span><span class="s1">&#39;e:\test_python&#39;</span><span class="p">)</span>
<span class="c1"># 以列表的方式列出指定目录的内容</span>
<span class="n">os</span><span class="o">.</span><span class="n">listdir</span><span class="p">(</span><span class="s1">&#39;.&#39;</span><span class="p">)</span>
<span class="c1"># 删除指定文件</span>
<span class="n">os</span><span class="o">.</span><span class="n">remove</span><span class="p">(</span><span class="sa">r</span><span class="s1">&#39;e:\test.txt&#39;</span><span class="p">)</span>
<span class="c1"># 把test.txt重命名为test1.txt</span>
<span class="n">os</span><span class="o">.</span><span class="n">rename</span><span class="p">(</span><span class="sa">r</span><span class="s1">&#39;e:\test.txt&#39;</span><span class="p">,</span> <span class="sa">r</span><span class="s1">&#39;e:\test1.txt&#39;</span><span class="p">)</span>
<span class="c1"># 查看文件的状态信息</span>
<span class="n">os</span><span class="o">.</span><span class="n">stat</span><span class="p">(</span><span class="sa">r</span><span class="s1">&#39;e:\test.dcm&#39;</span><span class="p">)</span>
<span class="s2">&#34;&#34;&#34;
</span><span class="s2">os.stat_result(st_mode=33206, st_ino=3659174697270772, 
</span><span class="s2">st_dev=505418071, st_nlink=1, st_uid=0, st_gid=0, 
</span><span class="s2">st_size=19468, st_atime=1515546652, st_mtime=1515546652, 
</span><span class="s2">st_ctime=1515546652)
</span><span class="s2">&#34;&#34;&#34;</span>
<span class="c1"># 当前操作系统的路径分隔符</span>
<span class="nb">print</span><span class="p">(</span><span class="n">os</span><span class="o">.</span><span class="n">sep</span><span class="p">)</span>
<span class="c1"># 当前操作系统的换行符</span>
<span class="nb">print</span><span class="p">(</span><span class="n">os</span><span class="o">.</span><span class="n">linesep</span><span class="p">)</span>
<span class="c1"># 环境变量之间的分隔符</span>
<span class="nb">print</span><span class="p">(</span><span class="n">os</span><span class="o">.</span><span class="n">pathsep</span><span class="p">)</span>
<span class="c1"># 查看系统的环境变量</span>
<span class="nb">print</span><span class="p">(</span><span class="n">os</span><span class="o">.</span><span class="n">environ</span><span class="p">)</span>
<span class="c1"># 查看当前系统的名称</span>
<span class="nb">print</span><span class="p">(</span><span class="n">os</span><span class="o">.</span><span class="n">name</span><span class="p">)</span>
<span class="c1"># 运行shell命令,直接显示结果</span>
<span class="c1"># print(os.system(&#39;dir&#39;))</span>


<span class="c1"># 返回path规范化的绝对路径</span>
<span class="nb">print</span><span class="p">(</span><span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">abspath</span><span class="p">(</span><span class="vm">__file__</span><span class="p">))</span>
<span class="c1"># 把路径分割成目录和文件名二元组返回</span>
<span class="nb">print</span><span class="p">(</span><span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="vm">__file__</span><span class="p">))</span>
<span class="c1"># 返回path的目录，其实就是os.path.split(path)的第一个元素</span>
<span class="nb">print</span><span class="p">(</span><span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">dirname</span><span class="p">(</span><span class="vm">__file__</span><span class="p">))</span>
<span class="c1"># 返回path的最后的文件名，如果path不是文件则返回空，其实就是os.path.split(path)的第二个元素</span>
<span class="nb">print</span><span class="p">(</span><span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">basename</span><span class="p">(</span><span class="vm">__file__</span><span class="p">))</span>
<span class="c1">#  如果path存在，返回True；如果path不存在，返回False</span>
<span class="nb">print</span><span class="p">(</span><span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">exists</span><span class="p">(</span><span class="vm">__file__</span><span class="p">))</span>
<span class="c1"># 如果path是绝对路径，返回True</span>
<span class="nb">print</span><span class="p">(</span><span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">isabs</span><span class="p">(</span><span class="vm">__file__</span><span class="p">))</span>
<span class="c1"># 如果path是一个存在的文件，返回True。否则返回False</span>
<span class="nb">print</span><span class="p">(</span><span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">isfile</span><span class="p">(</span><span class="vm">__file__</span><span class="p">))</span>
<span class="c1"># 如果path是一个存在的目录，则返回True。否则返回False</span>
<span class="nb">print</span><span class="p">(</span><span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">isdir</span><span class="p">(</span><span class="vm">__file__</span><span class="p">))</span>
<span class="c1"># 将多个路径组合后返回，第一个绝对路径之前的参数将被忽略</span>
<span class="nb">print</span><span class="p">(</span><span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="s1">&#39;c:</span><span class="se">\\</span><span class="s1">&#39;</span><span class="p">,</span> <span class="s1">&#39;a&#39;</span><span class="p">,</span> <span class="s1">&#39;b&#39;</span><span class="p">))</span>
<span class="c1"># 返回path所指向的文件或者目录的最后存取时间</span>
<span class="nb">print</span><span class="p">(</span><span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">getatime</span><span class="p">(</span><span class="vm">__file__</span><span class="p">))</span>
<span class="c1">#  返回path所指向的文件或者目录的最后修改时间</span>
<span class="nb">print</span><span class="p">(</span><span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">getmtime</span><span class="p">(</span><span class="vm">__file__</span><span class="p">))</span>
</code></pre></td></tr></table>
</div>
</div><h3 id="shutil">shutil</h3>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span><span class="lnt">14
</span><span class="lnt">15
</span><span class="lnt">16
</span><span class="lnt">17
</span><span class="lnt">18
</span><span class="lnt">19
</span><span class="lnt">20
</span><span class="lnt">21
</span><span class="lnt">22
</span><span class="lnt">23
</span><span class="lnt">24
</span><span class="lnt">25
</span><span class="lnt">26
</span><span class="lnt">27
</span><span class="lnt">28
</span><span class="lnt">29
</span><span class="lnt">30
</span><span class="lnt">31
</span><span class="lnt">32
</span><span class="lnt">33
</span><span class="lnt">34
</span><span class="lnt">35
</span><span class="lnt">36
</span><span class="lnt">37
</span><span class="lnt">38
</span><span class="lnt">39
</span><span class="lnt">40
</span><span class="lnt">41
</span><span class="lnt">42
</span><span class="lnt">43
</span><span class="lnt">44
</span><span class="lnt">45
</span><span class="lnt">46
</span><span class="lnt">47
</span><span class="lnt">48
</span><span class="lnt">49
</span><span class="lnt">50
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-python" data-lang="python"><span class="kn">import</span> <span class="nn">shutil</span>
<span class="s2">&#34;&#34;&#34;
</span><span class="s2">主要作用是拷贝文件、拷贝文件的权限、状态信息以及压缩文件、移动文件、删除文件
</span><span class="s2">&#34;&#34;&#34;</span>
 
<span class="c1"># 将一个文件对象的内容拷贝到另一个文件对象中,可以部分内容</span>
<span class="s2">&#34;&#34;&#34;
</span><span class="s2">f1 = open(r&#39;E:\pythondata\day05</span><span class="se">\t</span><span class="s2">est.txt&#39;, &#39;rb&#39;)
</span><span class="s2">f2 = open(r&#39;E:\pythondata\day05</span><span class="se">\t</span><span class="s2">est1.txt&#39;, &#39;wb&#39;)
</span><span class="s2">shutil.copyfileobj(f1, f2)
</span><span class="s2">shutil.copyfileobj(f1, f2, 29) #部分拷贝
</span><span class="s2">&#34;&#34;&#34;</span>
<span class="c1"># 不用创建文件对象，直接用文件的路径实现文件的拷贝</span>
<span class="s2">&#34;&#34;&#34;
</span><span class="s2">shutil.copyfile(r&#39;E:\pythondata\day05</span><span class="se">\t</span><span class="s2">est.txt&#39;, r&#39;E:\pythondata\day05</span><span class="se">\t</span><span class="s2">est1.txt&#39;)
</span><span class="s2">&#34;&#34;&#34;</span>
<span class="c1"># 将源文件的权限信息拷贝到目标文件</span>
<span class="s2">&#34;&#34;&#34;
</span><span class="s2">shutil.copymode(r&#39;E:\pythondata\day05</span><span class="se">\t</span><span class="s2">est.txt&#39;, r&#39;E:\pythondata\day05</span><span class="se">\t</span><span class="s2">est1.txt&#39;)
</span><span class="s2">&#34;&#34;&#34;</span>
<span class="c1"># 将源文件的所有状态信息都拷贝到目标文件</span>
<span class="s2">&#34;&#34;&#34;
</span><span class="s2">shutil.copystat(r&#39;E:\pythondata\day05</span><span class="se">\t</span><span class="s2">est.txt&#39;, r&#39;E:\pythondata\day05</span><span class="se">\t</span><span class="s2">est1.txt&#39;)
</span><span class="s2">&#34;&#34;&#34;</span>
<span class="c1"># 同时拷贝文件内容和权限</span>
<span class="s2">&#34;&#34;&#34;
</span><span class="s2">shutil.copy(r&#39;E:\pythondata\day05</span><span class="se">\t</span><span class="s2">est.txt&#39;, r&#39;E:\pythondata\day05</span><span class="se">\t</span><span class="s2">est1.txt&#39;)
</span><span class="s2">&#34;&#34;&#34;</span>
<span class="c1"># 同时拷贝文件内容和所有的状态信息</span>
<span class="s2">&#34;&#34;&#34;
</span><span class="s2">shutil.copy2(r&#39;E:\pythondata\day05</span><span class="se">\t</span><span class="s2">est.txt&#39;, r&#39;E:\pythondata\day05</span><span class="se">\t</span><span class="s2">est1.txt&#39;)
</span><span class="s2">&#34;&#34;&#34;</span>
<span class="c1"># 拷贝目录下所有的内容到新的目录下，递归方式的拷贝</span>
<span class="s2">&#34;&#34;&#34;
</span><span class="s2">shutil.copytree(r&#34;e:</span><span class="se">\t</span><span class="s2">est&#34;, r&#34;e:</span><span class="se">\t</span><span class="s2">est2&#34;)
</span><span class="s2">&#34;&#34;&#34;</span>
<span class="c1"># 删除目录并清空下面的所有内容</span>
<span class="s2">&#34;&#34;&#34;
</span><span class="s2">shutil.rmtree(r&#39;e:</span><span class="se">\t</span><span class="s2">est2&#39;)
</span><span class="s2">&#34;&#34;&#34;</span>
<span class="c1"># 移动文件夹即下面的内容到新的文件夹中</span>
<span class="s2">&#34;&#34;&#34;
</span><span class="s2">shutil.move(r&#39;e:</span><span class="se">\t</span><span class="s2">est&#39;, r&#39;e:</span><span class="se">\t</span><span class="s2">est2&#39;)
</span><span class="s2">&#34;&#34;&#34;</span>
<span class="c1"># 将指定的文件夹压缩  zip|tar|bztar|gztar</span>
<span class="c1"># shutil 对压缩包的处理是通过调用ZipFile 和 TarFile两个模块来进行的。</span>
<span class="s2">&#34;&#34;&#34;
</span><span class="s2">ret = shutil.make_archive(r&#39;E:\pythondata\day05\wwwwwwww&#39;, &#39;gztar&#39;, root_dir=r&#39;E:\pythondata\day05&#39;)
</span><span class="s2">&#34;&#34;&#34;</span>

</code></pre></td></tr></table>
</div>
</div><div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-python" data-lang="python"><span class="kn">import</span> <span class="nn">zipfile</span>
 
<span class="c1"># 压缩</span>
<span class="n">z</span> <span class="o">=</span> <span class="n">zipfile</span><span class="o">.</span><span class="n">ZipFile</span><span class="p">(</span><span class="s1">&#39;laxi.zip&#39;</span><span class="p">,</span> <span class="s1">&#39;w&#39;</span><span class="p">)</span>
<span class="n">z</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s1">&#39;a.log&#39;</span><span class="p">)</span>
<span class="n">z</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s1">&#39;data.data&#39;</span><span class="p">)</span>
<span class="n">z</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
 
<span class="c1"># 解压</span>
<span class="n">z</span> <span class="o">=</span> <span class="n">zipfile</span><span class="o">.</span><span class="n">ZipFile</span><span class="p">(</span><span class="s1">&#39;laxi.zip&#39;</span><span class="p">,</span> <span class="s1">&#39;r&#39;</span><span class="p">)</span>
<span class="n">z</span><span class="o">.</span><span class="n">extractall</span><span class="p">()</span>
<span class="n">z</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
</code></pre></td></tr></table>
</div>
</div><div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-python" data-lang="python"><span class="kn">import</span> <span class="nn">tarfile</span>
 
<span class="c1"># 压缩</span>
<span class="n">tar</span> <span class="o">=</span> <span class="n">tarfile</span><span class="o">.</span><span class="n">open</span><span class="p">(</span><span class="s1">&#39;your.tar&#39;</span><span class="p">,</span><span class="s1">&#39;w&#39;</span><span class="p">)</span>
<span class="n">tar</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="s1">&#39;/Users/wupeiqi/PycharmProjects/bbs2.log&#39;</span><span class="p">,</span> <span class="n">arcname</span><span class="o">=</span><span class="s1">&#39;bbs2.log&#39;</span><span class="p">)</span>
<span class="n">tar</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="s1">&#39;/Users/wupeiqi/PycharmProjects/cmdb.log&#39;</span><span class="p">,</span> <span class="n">arcname</span><span class="o">=</span><span class="s1">&#39;cmdb.log&#39;</span><span class="p">)</span>
<span class="n">tar</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
 
<span class="c1"># 解压</span>
<span class="n">tar</span> <span class="o">=</span> <span class="n">tarfile</span><span class="o">.</span><span class="n">open</span><span class="p">(</span><span class="s1">&#39;your.tar&#39;</span><span class="p">,</span><span class="s1">&#39;r&#39;</span><span class="p">)</span>
<span class="n">tar</span><span class="o">.</span><span class="n">extractall</span><span class="p">()</span>  <span class="c1"># 可设置解压地址</span>
<span class="n">tar</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
</code></pre></td></tr></table>
</div>
</div><h3 id="urllib库">urllib库</h3>
<p><a href="https://docs.python.org/3.7/library/urllib.html">urllib</a><br>
整个模块分为:</p>
<ul>
<li>urllib.request,</li>
<li>urllib.parse,</li>
<li>urllib.error</li>
</ul>
<h4 id="1-基本用法urlopen">1. 基本用法urlopen</h4>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span><span class="lnt">14
</span><span class="lnt">15
</span><span class="lnt">16
</span><span class="lnt">17
</span><span class="lnt">18
</span><span class="lnt">19
</span><span class="lnt">20
</span><span class="lnt">21
</span><span class="lnt">22
</span><span class="lnt">23
</span><span class="lnt">24
</span><span class="lnt">25
</span><span class="lnt">26
</span><span class="lnt">27
</span><span class="lnt">28
</span><span class="lnt">29
</span><span class="lnt">30
</span><span class="lnt">31
</span><span class="lnt">32
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-python" data-lang="python"><span class="n">urllib</span><span class="o">.</span><span class="n">request</span><span class="o">.</span><span class="n">urlopen</span><span class="p">(</span><span class="n">url</span><span class="p">,</span> <span class="n">data</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="p">[</span><span class="n">timeout</span><span class="p">,</span> <span class="p">]</span><span class="o">*</span><span class="p">,</span> <span class="n">cafile</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">capath</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">cadefault</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> <span class="n">context</span><span class="o">=</span><span class="kc">None</span><span class="p">)</span>
<span class="s1">&#39;&#39;&#39;
</span><span class="s1">url:  需要打开的网址
</span><span class="s1">data：Post提交的数据
</span><span class="s1">timeout：设置网站的访问超时时间
</span><span class="s1">&#39;&#39;&#39;</span>
<span class="kn">from</span> <span class="nn">urllib</span> <span class="kn">import</span> <span class="n">request</span>
<span class="c1"># &lt;http.client.HTTPResponse object at 0x00000000048BC908&gt; HTTPResponse类型</span>
<span class="n">response</span> <span class="o">=</span> <span class="n">request</span><span class="o">.</span><span class="n">urlopen</span><span class="p">(</span><span class="sa">r</span><span class="s1">&#39;http://python.org/&#39;</span><span class="p">)</span> 


<span class="n">page</span> <span class="o">=</span> <span class="n">response</span><span class="o">.</span><span class="n">read</span><span class="p">()</span>
<span class="n">html</span> <span class="o">=</span> <span class="n">page</span><span class="o">.</span><span class="n">decode</span><span class="p">(</span><span class="s1">&#39;utf-8&#39;</span><span class="p">)</span> <span class="c1"># 返回html源码</span>

<span class="c1"># response对象提供方法</span>
<span class="n">page</span><span class="o">.</span><span class="n">readline</span><span class="p">()</span>
<span class="n">page</span><span class="o">.</span><span class="n">info</span><span class="p">()</span>
<span class="n">page</span><span class="o">.</span><span class="n">getcode</span><span class="p">()</span>
<span class="n">page</span><span class="o">.</span><span class="n">geturl</span><span class="p">()</span>
<span class="s1">&#39;&#39;&#39;
</span><span class="s1">read() , readline() ,readlines() , fileno() , close() ：对HTTPResponse类型数据进行操作
</span><span class="s1">info()：返回HTTPMessage对象，表示远程服务器返回的头信息
</span><span class="s1">getcode()：返回Http状态码。如果是http请求，200请求成功完成;404网址未找到
</span><span class="s1">geturl()：返回请求的url
</span><span class="s1">&#39;&#39;&#39;</span>

<span class="c1"># 下载url的图片保存到path中</span>
<span class="n">urllib</span><span class="o">.</span><span class="n">urlretrieve</span><span class="p">(</span><span class="n">url</span><span class="p">[,</span> <span class="n">filename</span><span class="p">[,</span> <span class="n">reporthook</span><span class="p">[,</span> <span class="n">data</span><span class="p">]]])</span>
<span class="n">request</span><span class="o">.</span><span class="n">urlretrieve</span><span class="p">(</span><span class="n">url</span><span class="p">,</span> <span class="n">path</span><span class="p">)</span> 

<span class="c1"># 编码</span>
<span class="n">urllib</span><span class="o">.</span><span class="n">parse</span><span class="o">.</span><span class="n">quote</span><span class="p">(</span><span class="s1">&#39;中文&#39;</span><span class="p">,</span> <span class="s1">&#39;=/%&#39;</span><span class="p">)</span>
</code></pre></td></tr></table>
</div>
</div><h4 id="2-使用request">2. 使用Request</h4>
<p>使用request()来包装请求，再通过urlopen()获取页面</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span><span class="lnt">14
</span><span class="lnt">15
</span><span class="lnt">16
</span><span class="lnt">17
</span><span class="lnt">18
</span><span class="lnt">19
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-python" data-lang="python"><span class="n">urllib</span><span class="o">.</span><span class="n">request</span><span class="o">.</span><span class="n">Request</span><span class="p">(</span><span class="n">url</span><span class="p">,</span> <span class="n">data</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">headers</span><span class="o">=</span><span class="p">{},</span> <span class="n">method</span><span class="o">=</span><span class="kc">None</span><span class="p">)</span>


<span class="n">url</span> <span class="o">=</span> <span class="sa">r</span><span class="s1">&#39;http://www.lagou.com/zhaopin/Python/?labelWords=label&#39;</span>
<span class="n">headers</span> <span class="o">=</span> <span class="p">{</span>
    <span class="s1">&#39;User-Agent&#39;</span><span class="p">:</span> <span class="sa">r</span><span class="s1">&#39;Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) &#39;</span>
                  <span class="sa">r</span><span class="s1">&#39;Chrome/45.0.2454.85 Safari/537.36 115Browser/6.0.3&#39;</span><span class="p">,</span>
    <span class="s1">&#39;Referer&#39;</span><span class="p">:</span> <span class="sa">r</span><span class="s1">&#39;http://www.lagou.com/zhaopin/Python/?labelWords=label&#39;</span><span class="p">,</span>
    <span class="s1">&#39;Connection&#39;</span><span class="p">:</span> <span class="s1">&#39;keep-alive&#39;</span>
<span class="p">}</span>

<span class="s1">&#39;&#39;&#39;
</span><span class="s1">- User-Agent ：这个头部可以携带如下几条信息：浏览器名和版本号、操作系统名和版本号、默认语言
</span><span class="s1">- Referer：可以用来防止盗链，有一些网站图片显示来源http://***.com，就是检查Referer来鉴定的
</span><span class="s1">- Connection：表示连接状态，记录Session的状态
</span><span class="s1">&#39;&#39;&#39;</span>
<span class="n">req</span> <span class="o">=</span> <span class="n">request</span><span class="o">.</span><span class="n">Request</span><span class="p">(</span><span class="n">url</span><span class="p">,</span> <span class="n">headers</span><span class="o">=</span><span class="n">headers</span><span class="p">)</span>
<span class="n">page</span> <span class="o">=</span> <span class="n">request</span><span class="o">.</span><span class="n">urlopen</span><span class="p">(</span><span class="n">req</span><span class="p">)</span><span class="o">.</span><span class="n">read</span><span class="p">()</span>
<span class="n">page</span> <span class="o">=</span> <span class="n">page</span><span class="o">.</span><span class="n">decode</span><span class="p">(</span><span class="s1">&#39;utf-8&#39;</span><span class="p">)</span>
</code></pre></td></tr></table>
</div>
</div><h4 id="3-post数据">3. POST数据</h4>
<p>urlopen()的data参数默认为None，当data参数不为空的时候，urlopen()提交方式为Post。</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span><span class="lnt">14
</span><span class="lnt">15
</span><span class="lnt">16
</span><span class="lnt">17
</span><span class="lnt">18
</span><span class="lnt">19
</span><span class="lnt">20
</span><span class="lnt">21
</span><span class="lnt">22
</span><span class="lnt">23
</span><span class="lnt">24
</span><span class="lnt">25
</span><span class="lnt">26
</span><span class="lnt">27
</span><span class="lnt">28
</span><span class="lnt">29
</span><span class="lnt">30
</span><span class="lnt">31
</span><span class="lnt">32
</span><span class="lnt">33
</span><span class="lnt">34
</span><span class="lnt">35
</span><span class="lnt">36
</span><span class="lnt">37
</span><span class="lnt">38
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-python" data-lang="python"><span class="n">urllib</span><span class="o">.</span><span class="n">request</span><span class="o">.</span><span class="n">urlopen</span><span class="p">(</span><span class="n">url</span><span class="p">,</span> <span class="n">data</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="p">[</span><span class="n">timeout</span><span class="p">,</span> <span class="p">]</span><span class="o">*</span><span class="p">,</span> 
    <span class="n">cafile</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">capath</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">cadefault</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> <span class="n">context</span><span class="o">=</span><span class="kc">None</span><span class="p">)</span>
<span class="s1">&#39;&#39;&#39;
</span><span class="s1">urlopen（）的data参数默认为None，当data参数不为空的时候，urlopen（）提交方式为Post。
</span><span class="s1">&#39;&#39;&#39;</span>

<span class="kn">from</span> <span class="nn">urllib</span> <span class="kn">import</span> <span class="n">request</span><span class="p">,</span> <span class="n">parse</span>
<span class="n">url</span> <span class="o">=</span> <span class="sa">r</span><span class="s1">&#39;http://www.lagou.com/jobs/positionAjax.json?&#39;</span>
<span class="n">headers</span> <span class="o">=</span> <span class="p">{</span>
    <span class="s1">&#39;User-Agent&#39;</span><span class="p">:</span> <span class="sa">r</span><span class="s1">&#39;Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) &#39;</span>
                  <span class="sa">r</span><span class="s1">&#39;Chrome/45.0.2454.85 Safari/537.36 115Browser/6.0.3&#39;</span><span class="p">,</span>
    <span class="s1">&#39;Referer&#39;</span><span class="p">:</span> <span class="sa">r</span><span class="s1">&#39;http://www.lagou.com/zhaopin/Python/?labelWords=label&#39;</span><span class="p">,</span>
    <span class="s1">&#39;Connection&#39;</span><span class="p">:</span> <span class="s1">&#39;keep-alive&#39;</span>
<span class="p">}</span>
<span class="n">data</span> <span class="o">=</span> <span class="p">{</span>
    <span class="s1">&#39;first&#39;</span><span class="p">:</span> <span class="s1">&#39;true&#39;</span><span class="p">,</span>
    <span class="s1">&#39;pn&#39;</span><span class="p">:</span> <span class="mi">1</span><span class="p">,</span>
    <span class="s1">&#39;kd&#39;</span><span class="p">:</span> <span class="s1">&#39;Python&#39;</span>
<span class="p">}</span>
<span class="n">data</span> <span class="o">=</span> <span class="n">parse</span><span class="o">.</span><span class="n">urlencode</span><span class="p">(</span><span class="n">data</span><span class="p">)</span><span class="o">.</span><span class="n">encode</span><span class="p">(</span><span class="s1">&#39;utf-8&#39;</span><span class="p">)</span>
<span class="n">req</span> <span class="o">=</span> <span class="n">request</span><span class="o">.</span><span class="n">Request</span><span class="p">(</span><span class="n">url</span><span class="p">,</span> <span class="n">headers</span><span class="o">=</span><span class="n">headers</span><span class="p">,</span> <span class="n">data</span><span class="o">=</span><span class="n">data</span><span class="p">)</span>
<span class="n">page</span> <span class="o">=</span> <span class="n">request</span><span class="o">.</span><span class="n">urlopen</span><span class="p">(</span><span class="n">req</span><span class="p">)</span><span class="o">.</span><span class="n">read</span><span class="p">()</span>
<span class="n">page</span> <span class="o">=</span> <span class="n">page</span><span class="o">.</span><span class="n">decode</span><span class="p">(</span><span class="s1">&#39;utf-8&#39;</span><span class="p">)</span>

<span class="c1"># urlencode() 主要作用就是将url附上要提交的数据。 </span>
<span class="n">urllib</span><span class="o">.</span><span class="n">parse</span><span class="o">.</span><span class="n">urlencode</span><span class="p">(</span><span class="n">query</span><span class="p">,</span> <span class="n">doseq</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> <span class="n">safe</span><span class="o">=</span><span class="s1">&#39;&#39;</span><span class="p">,</span> <span class="n">encoding</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">errors</span><span class="o">=</span><span class="kc">None</span><span class="p">)</span>
<span class="n">data</span> <span class="o">=</span> <span class="p">{</span>
    <span class="s1">&#39;first&#39;</span><span class="p">:</span> <span class="s1">&#39;true&#39;</span><span class="p">,</span>
    <span class="s1">&#39;pn&#39;</span><span class="p">:</span> <span class="mi">1</span><span class="p">,</span>
    <span class="s1">&#39;kd&#39;</span><span class="p">:</span> <span class="s1">&#39;Python&#39;</span>
<span class="p">}</span>
<span class="n">data</span> <span class="o">=</span> <span class="n">parse</span><span class="o">.</span><span class="n">urlencode</span><span class="p">(</span><span class="n">data</span><span class="p">)</span><span class="o">.</span><span class="n">encode</span><span class="p">(</span><span class="s1">&#39;utf-8&#39;</span><span class="p">)</span>
<span class="s1">&#39;&#39;&#39;
</span><span class="s1">经过urlencode（）转换后的data数据为?first=true?pn=1?kd=Python，最后提交的url为
</span><span class="s1">http://www.lagou.com/jobs/positionAjax.json?first=true?pn=1?kd=Python
</span><span class="s1">Post的数据必须是bytes或者iterable of bytes，不能是str，因此需要进行encode（）编码
</span><span class="s1">&#39;&#39;&#39;</span>
<span class="n">page</span> <span class="o">=</span> <span class="n">request</span><span class="o">.</span><span class="n">urlopen</span><span class="p">(</span><span class="n">req</span><span class="p">,</span> <span class="n">data</span><span class="o">=</span><span class="n">data</span><span class="p">)</span><span class="o">.</span><span class="n">read</span><span class="p">()</span>
</code></pre></td></tr></table>
</div>
</div><h4 id="4-urllibparse">4. urllib.parse</h4>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span><span class="lnt">14
</span><span class="lnt">15
</span><span class="lnt">16
</span><span class="lnt">17
</span><span class="lnt">18
</span><span class="lnt">19
</span><span class="lnt">20
</span><span class="lnt">21
</span><span class="lnt">22
</span><span class="lnt">23
</span><span class="lnt">24
</span><span class="lnt">25
</span><span class="lnt">26
</span><span class="lnt">27
</span><span class="lnt">28
</span><span class="lnt">29
</span><span class="lnt">30
</span><span class="lnt">31
</span><span class="lnt">32
</span><span class="lnt">33
</span><span class="lnt">34
</span><span class="lnt">35
</span><span class="lnt">36
</span><span class="lnt">37
</span><span class="lnt">38
</span><span class="lnt">39
</span><span class="lnt">40
</span><span class="lnt">41
</span><span class="lnt">42
</span><span class="lnt">43
</span><span class="lnt">44
</span><span class="lnt">45
</span><span class="lnt">46
</span><span class="lnt">47
</span><span class="lnt">48
</span><span class="lnt">49
</span><span class="lnt">50
</span><span class="lnt">51
</span><span class="lnt">52
</span><span class="lnt">53
</span><span class="lnt">54
</span><span class="lnt">55
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-python" data-lang="python"><span class="kn">import</span> <span class="nn">urllib.parse</span>
 
<span class="c1"># 解析url，返回包含url信息的6元元组</span>
<span class="n">res</span><span class="o">=</span><span class="n">urllib</span><span class="o">.</span><span class="n">parse</span><span class="o">.</span><span class="n">urlparse</span><span class="p">(</span><span class="s2">&#34;https://docs.python.org/3/library/urllib.parse.html#module-urllib.parse&#34;</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="n">res</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="n">res</span><span class="o">.</span><span class="n">scheme</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="n">res</span><span class="o">.</span><span class="n">netloc</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="n">res</span><span class="o">.</span><span class="n">path</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="n">res</span><span class="o">.</span><span class="n">params</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="n">res</span><span class="o">.</span><span class="n">query</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="n">res</span><span class="o">.</span><span class="n">fragment</span><span class="p">)</span>
 
<span class="c1"># 从任意包含6元素的可迭代数据结构中构造url字符串并返回</span>
<span class="n">url</span><span class="o">=</span><span class="n">urllib</span><span class="o">.</span><span class="n">parse</span><span class="o">.</span><span class="n">urlunparse</span><span class="p">([</span><span class="s2">&#34;http&#34;</span><span class="p">,</span><span class="s2">&#34;www.baidu.com&#34;</span><span class="p">,</span><span class="s2">&#34;index&#34;</span><span class="p">,</span><span class="s2">&#34;&#34;</span><span class="p">,</span><span class="s2">&#34;&#34;</span><span class="p">,</span><span class="s2">&#34;&#34;</span><span class="p">])</span>
<span class="nb">print</span><span class="p">(</span><span class="n">url</span><span class="p">)</span>
 
<span class="c1"># 分割url，返回包含url的元组，可用来替代urlparse()</span>
<span class="n">res</span><span class="o">=</span><span class="n">urllib</span><span class="o">.</span><span class="n">parse</span><span class="o">.</span><span class="n">urlsplit</span><span class="p">(</span><span class="s2">&#34;https://docs.python.org/3/library/urllib.parse.html#module-urllib.parse&#34;</span><span class="p">,</span><span class="n">scheme</span><span class="o">=</span><span class="s2">&#34;https&#34;</span><span class="p">,</span><span class="n">allow_fragments</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="n">res</span><span class="p">)</span>
<span class="c1"># 从SplitResult中获取url字符串</span>
<span class="nb">print</span><span class="p">(</span><span class="n">res</span><span class="o">.</span><span class="n">geturl</span><span class="p">())</span>
 
<span class="c1"># 从任意包含5元素的可迭代数据结构中构造url字符串并返回，可用来替代urlunparse()</span>
<span class="n">url</span><span class="o">=</span><span class="n">urllib</span><span class="o">.</span><span class="n">parse</span><span class="o">.</span><span class="n">urlunsplit</span><span class="p">([</span><span class="s2">&#34;http&#34;</span><span class="p">,</span><span class="s2">&#34;x.org&#34;</span><span class="p">,</span><span class="s2">&#34;index.html&#34;</span><span class="p">,</span><span class="s2">&#34;&#34;</span><span class="p">,</span><span class="s2">&#34;&#34;</span><span class="p">])</span>
<span class="nb">print</span><span class="p">(</span><span class="n">url</span><span class="p">)</span>
 
<span class="c1"># 从2个url中构造完整的url</span>
<span class="n">url</span><span class="o">=</span><span class="n">urllib</span><span class="o">.</span><span class="n">parse</span><span class="o">.</span><span class="n">urljoin</span><span class="p">(</span><span class="n">base</span><span class="o">=</span><span class="s2">&#34;http://www.baidu.com&#34;</span><span class="p">,</span><span class="n">url</span><span class="o">=</span><span class="s2">&#34;index.html#a&#34;</span><span class="p">,</span><span class="n">allow_fragments</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="n">url</span><span class="p">)</span>
<span class="n">url</span><span class="o">=</span><span class="n">urllib</span><span class="o">.</span><span class="n">parse</span><span class="o">.</span><span class="n">urljoin</span><span class="p">(</span><span class="n">base</span><span class="o">=</span><span class="s2">&#34;http://www.baidu.com/&#34;</span><span class="p">,</span><span class="n">url</span><span class="o">=</span><span class="s2">&#34;//www.baidu.com/index.html&#34;</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="n">url</span><span class="p">)</span>
 
<span class="c1"># 对含有特殊符号的URL进行编码，使其转换为合法的url字符串</span>
<span class="n">url</span><span class="o">=</span><span class="n">urllib</span><span class="o">.</span><span class="n">parse</span><span class="o">.</span><span class="n">quote</span><span class="p">(</span><span class="s2">&#34;https://www.baidu.com/s?wd=编程 python&#34;</span><span class="p">,</span><span class="n">safe</span><span class="o">=</span><span class="s2">&#34;/&#34;</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="n">url</span><span class="p">)</span>
<span class="c1"># 效果与quote()唯一不同的是，将空格转换为加号 +</span>
<span class="n">url</span><span class="o">=</span><span class="n">urllib</span><span class="o">.</span><span class="n">parse</span><span class="o">.</span><span class="n">quote_plus</span><span class="p">(</span><span class="s2">&#34;https://www.baidu.com/s?wd=编程 python&#34;</span><span class="p">,</span><span class="n">safe</span><span class="o">=</span><span class="s2">&#34;/&#34;</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="n">url</span><span class="p">)</span>
<span class="c1"># 与quote()相反</span>
<span class="n">url</span><span class="o">=</span><span class="n">urllib</span><span class="o">.</span><span class="n">parse</span><span class="o">.</span><span class="n">unquote</span><span class="p">(</span><span class="s2">&#34;https%3A//www.baidu.com/s</span><span class="si">%3F</span><span class="s2">wd%3D</span><span class="si">%E</span><span class="s2">7%BC</span><span class="si">%96%</span><span class="s2">E7%A8%8B%20python&#34;</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="n">url</span><span class="p">)</span>
<span class="c1"># 与quote_plus()相反</span>
<span class="n">url</span><span class="o">=</span><span class="n">urllib</span><span class="o">.</span><span class="n">parse</span><span class="o">.</span><span class="n">unquote_plus</span><span class="p">(</span><span class="s2">&#34;https%3A//www.baidu.com/s</span><span class="si">%3F</span><span class="s2">wd%3D</span><span class="si">%E</span><span class="s2">7%BC</span><span class="si">%96%</span><span class="s2">E7%A8%8B+python&#34;</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="n">url</span><span class="p">)</span>
 
<span class="c1"># url编码,第一个参数为dict，常用来处理url参数</span>
<span class="n">params</span><span class="o">=</span><span class="p">{</span><span class="s2">&#34;key1&#34;</span><span class="p">:</span><span class="s2">&#34;编程&#34;</span><span class="p">,</span><span class="s2">&#34;key2&#34;</span><span class="p">:</span><span class="s2">&#34;写作&#34;</span><span class="p">}</span>
<span class="n">params</span><span class="o">=</span><span class="n">urllib</span><span class="o">.</span><span class="n">parse</span><span class="o">.</span><span class="n">urlencode</span><span class="p">(</span><span class="n">params</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="n">params</span><span class="p">)</span>
<span class="c1"># url解码，返回字典，与urlencode()相反</span>
<span class="n">params</span><span class="o">=</span><span class="n">urllib</span><span class="o">.</span><span class="n">parse</span><span class="o">.</span><span class="n">parse_qs</span><span class="p">(</span><span class="n">urllib</span><span class="o">.</span><span class="n">parse</span><span class="o">.</span><span class="n">urlencode</span><span class="p">({</span><span class="s2">&#34;key&#34;</span><span class="p">:</span><span class="s2">&#34;编程&#34;</span><span class="p">}))</span>
<span class="nb">print</span><span class="p">(</span><span class="n">params</span><span class="p">)</span>
<span class="c1"># 与parse_qs()唯一的不同是返回列表</span>
<span class="n">params</span><span class="o">=</span><span class="n">urllib</span><span class="o">.</span><span class="n">parse</span><span class="o">.</span><span class="n">parse_qsl</span><span class="p">(</span><span class="n">urllib</span><span class="o">.</span><span class="n">parse</span><span class="o">.</span><span class="n">urlencode</span><span class="p">({</span><span class="s2">&#34;key1&#34;</span><span class="p">:</span><span class="s2">&#34;编程&#34;</span><span class="p">}))</span>
<span class="nb">print</span><span class="p">(</span><span class="n">params</span><span class="p">)</span>
</code></pre></td></tr></table>
</div>
</div><h4 id="5-异常处理">5. 异常处理</h4>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span><span class="lnt">14
</span><span class="lnt">15
</span><span class="lnt">16
</span><span class="lnt">17
</span><span class="lnt">18
</span><span class="lnt">19
</span><span class="lnt">20
</span><span class="lnt">21
</span><span class="lnt">22
</span><span class="lnt">23
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-python" data-lang="python"><span class="kn">from</span> <span class="nn">urllib</span> <span class="kn">import</span> <span class="n">parse</span>
<span class="k">def</span> <span class="nf">get_page</span><span class="p">(</span><span class="n">url</span><span class="p">):</span>
    <span class="n">headers</span> <span class="o">=</span> <span class="p">{</span>
        <span class="s1">&#39;User-Agent&#39;</span><span class="p">:</span> <span class="sa">r</span><span class="s1">&#39;Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) &#39;</span>
                    <span class="sa">r</span><span class="s1">&#39;Chrome/45.0.2454.85 Safari/537.36 115Browser/6.0.3&#39;</span><span class="p">,</span>
        <span class="s1">&#39;Referer&#39;</span><span class="p">:</span> <span class="sa">r</span><span class="s1">&#39;http://www.lagou.com/zhaopin/Python/?labelWords=label&#39;</span><span class="p">,</span>
        <span class="s1">&#39;Connection&#39;</span><span class="p">:</span> <span class="s1">&#39;keep-alive&#39;</span>
    <span class="p">}</span>
    <span class="n">data</span> <span class="o">=</span> <span class="p">{</span>
        <span class="s1">&#39;first&#39;</span><span class="p">:</span> <span class="s1">&#39;true&#39;</span><span class="p">,</span>
        <span class="s1">&#39;pn&#39;</span><span class="p">:</span> <span class="mi">1</span><span class="p">,</span>
        <span class="s1">&#39;kd&#39;</span><span class="p">:</span> <span class="s1">&#39;Python&#39;</span>
    <span class="p">}</span>
    <span class="n">data</span> <span class="o">=</span> <span class="n">parse</span><span class="o">.</span><span class="n">urlencode</span><span class="p">(</span><span class="n">data</span><span class="p">)</span><span class="o">.</span><span class="n">encode</span><span class="p">(</span><span class="s1">&#39;utf-8&#39;</span><span class="p">)</span> <span class="c1"># 编码参数</span>
    <span class="n">req</span> <span class="o">=</span> <span class="n">request</span><span class="o">.</span><span class="n">Request</span><span class="p">(</span><span class="n">url</span><span class="p">,</span> <span class="n">headers</span><span class="o">=</span><span class="n">headers</span><span class="p">)</span>
    <span class="k">try</span><span class="p">:</span>
        <span class="n">page</span> <span class="o">=</span> <span class="n">request</span><span class="o">.</span><span class="n">urlopen</span><span class="p">(</span><span class="n">req</span><span class="p">,</span> <span class="n">data</span><span class="o">=</span><span class="n">data</span><span class="p">)</span><span class="o">.</span><span class="n">read</span><span class="p">()</span>
        <span class="n">page</span> <span class="o">=</span> <span class="n">page</span><span class="o">.</span><span class="n">decode</span><span class="p">(</span><span class="s1">&#39;utf-8&#39;</span><span class="p">)</span>
    <span class="k">except</span> <span class="n">error</span><span class="o">.</span><span class="n">HTTPError</span> <span class="k">as</span> <span class="n">e</span><span class="p">:</span>
        <span class="nb">print</span><span class="p">(</span><span class="n">e</span><span class="o">.</span><span class="n">code</span><span class="p">())</span>
        <span class="nb">print</span><span class="p">(</span><span class="n">e</span><span class="o">.</span><span class="n">read</span><span class="p">()</span><span class="o">.</span><span class="n">decode</span><span class="p">(</span><span class="s1">&#39;utf-8&#39;</span><span class="p">))</span>
    <span class="k">return</span> <span class="n">page</span>

</code></pre></td></tr></table>
</div>
</div><h4 id="5-使用代理">5. 使用代理</h4>
<p>urllib.request.ProxyHandler(proxies=None)
当需要抓取的网站设置了访问限制，这时就需要用到代理来抓取数据。</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-python" data-lang="python"><span class="n">data</span> <span class="o">=</span> <span class="p">{</span>
        <span class="s1">&#39;first&#39;</span><span class="p">:</span> <span class="s1">&#39;true&#39;</span><span class="p">,</span>
        <span class="s1">&#39;pn&#39;</span><span class="p">:</span> <span class="mi">1</span><span class="p">,</span>
        <span class="s1">&#39;kd&#39;</span><span class="p">:</span> <span class="s1">&#39;Python&#39;</span>
    <span class="p">}</span>
<span class="n">proxy</span> <span class="o">=</span> <span class="n">request</span><span class="o">.</span><span class="n">ProxyHandler</span><span class="p">({</span><span class="s1">&#39;http&#39;</span><span class="p">:</span> <span class="s1">&#39;5.22.195.215:80&#39;</span><span class="p">})</span>  <span class="c1"># 设置proxy</span>
<span class="n">opener</span> <span class="o">=</span> <span class="n">request</span><span class="o">.</span><span class="n">build_opener</span><span class="p">(</span><span class="n">proxy</span><span class="p">)</span>  <span class="c1"># 挂载opener</span>
<span class="n">request</span><span class="o">.</span><span class="n">install_opener</span><span class="p">(</span><span class="n">opener</span><span class="p">)</span>  <span class="c1"># 安装opener</span>
<span class="n">data</span> <span class="o">=</span> <span class="n">parse</span><span class="o">.</span><span class="n">urlencode</span><span class="p">(</span><span class="n">data</span><span class="p">)</span><span class="o">.</span><span class="n">encode</span><span class="p">(</span><span class="s1">&#39;utf-8&#39;</span><span class="p">)</span>
<span class="n">page</span> <span class="o">=</span> <span class="n">opener</span><span class="o">.</span><span class="n">open</span><span class="p">(</span><span class="n">url</span><span class="p">,</span> <span class="n">data</span><span class="p">)</span><span class="o">.</span><span class="n">read</span><span class="p">()</span>
<span class="n">page</span> <span class="o">=</span> <span class="n">page</span><span class="o">.</span><span class="n">decode</span><span class="p">(</span><span class="s1">&#39;utf-8&#39;</span><span class="p">)</span>
<span class="k">return</span> <span class="n">page</span>
</code></pre></td></tr></table>
</div>
</div><h3 id="random模块">random模块</h3>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span><span class="lnt">14
</span><span class="lnt">15
</span><span class="lnt">16
</span><span class="lnt">17
</span><span class="lnt">18
</span><span class="lnt">19
</span><span class="lnt">20
</span><span class="lnt">21
</span><span class="lnt">22
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-python" data-lang="python"><span class="c1"># -*- coding:utf-8 -*-</span>
<span class="c1"># Author: Evan Mi</span>
<span class="kn">import</span> <span class="nn">random</span>
<span class="c1"># 产生一个 &gt;=0 并且 &lt;1 的随机浮点数</span>
<span class="nb">print</span><span class="p">(</span><span class="n">random</span><span class="o">.</span><span class="n">random</span><span class="p">())</span>
<span class="s2">&#34;&#34;&#34;
</span><span class="s2">Get a random number in the range [a, b) or [a, b] depending on rounding.
</span><span class="s2">获得一个[1,3)或者[1,3]范围内的随机浮点数，在哪个范围内依赖与rounding
</span><span class="s2">&#34;&#34;&#34;</span>
<span class="nb">print</span><span class="p">(</span><span class="n">random</span><span class="o">.</span><span class="n">uniform</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">3</span><span class="p">))</span>
<span class="c1"># 产生一个 &gt;=1 并且 &lt;=3 的随机整数</span>
<span class="nb">print</span><span class="p">(</span><span class="n">random</span><span class="o">.</span><span class="n">randint</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">3</span><span class="p">))</span>
<span class="c1"># 从1开始进行随机次数的+3操作；结果要小于5</span>
<span class="nb">print</span><span class="p">(</span><span class="n">random</span><span class="o">.</span><span class="n">randrange</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">5</span><span class="p">,</span> <span class="mi">3</span><span class="p">))</span>
<span class="c1"># 从给定的字符串、列表、元组中随机挑选一个值</span>
<span class="nb">print</span><span class="p">(</span><span class="n">random</span><span class="o">.</span><span class="n">choice</span><span class="p">([</span><span class="mi">1</span><span class="p">,</span> <span class="mi">5</span><span class="p">,</span> <span class="mi">9</span><span class="p">,</span> <span class="mi">6</span><span class="p">,</span> <span class="mi">3</span><span class="p">]))</span>
<span class="c1"># 从给定的序列中（字符串、列表、元组）随机取指定个数的样本</span>
<span class="nb">print</span><span class="p">(</span><span class="n">random</span><span class="o">.</span><span class="n">sample</span><span class="p">(</span><span class="s1">&#39;hello&#39;</span><span class="p">,</span> <span class="mi">2</span><span class="p">))</span>
<span class="n">x</span> <span class="o">=</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">4</span><span class="p">,</span> <span class="mi">5</span><span class="p">,</span> <span class="mi">6</span><span class="p">]</span>
<span class="c1"># 打乱顺序，就像洗牌一样</span>
<span class="n">random</span><span class="o">.</span><span class="n">shuffle</span><span class="p">(</span><span class="n">x</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="n">x</span><span class="p">)</span>
</code></pre></td></tr></table>
</div>
</div><h3 id="time和datetime模块">time和datetime模块</h3>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span><span class="lnt">14
</span><span class="lnt">15
</span><span class="lnt">16
</span><span class="lnt">17
</span><span class="lnt">18
</span><span class="lnt">19
</span><span class="lnt">20
</span><span class="lnt">21
</span><span class="lnt">22
</span><span class="lnt">23
</span><span class="lnt">24
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-python" data-lang="python"><span class="c1"># -*- coding:utf-8 -*-</span>
<span class="c1"># Author: Evan Mi</span>
<span class="kn">import</span> <span class="nn">time</span>
<span class="c1"># 获得时间戳,当前时区的</span>
<span class="nb">print</span><span class="p">(</span><span class="n">time</span><span class="o">.</span><span class="n">time</span><span class="p">())</span>
<span class="c1"># 不传参数获得格林威治时间tuple，传入秒数的话就是把秒数转为tuple</span>
<span class="nb">print</span><span class="p">(</span><span class="n">time</span><span class="o">.</span><span class="n">gmtime</span><span class="p">())</span>
<span class="c1"># 不传参数将获得当前时区的时间tuple,传入秒数的话就是把秒数转为tuple</span>
<span class="nb">print</span><span class="p">(</span><span class="n">time</span><span class="o">.</span><span class="n">localtime</span><span class="p">())</span>
<span class="s2">&#34;&#34;&#34;
</span><span class="s2">time.struct_time(tm_year=2018, tm_mon=4, tm_mday=4, tm_hour=8, tm_min=47, tm_sec=57, tm_wday=2, tm_yday=94, tm_isdst=0)
</span><span class="s2">可以通过time.localtime().tm_year 或其他字段来直接获得相应字段的值
</span><span class="s2">&#34;&#34;&#34;</span>
<span class="c1"># 把元组转为秒</span>
<span class="n">x</span> <span class="o">=</span> <span class="n">time</span><span class="o">.</span><span class="n">gmtime</span><span class="p">(</span><span class="mi">1245554451</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="n">time</span><span class="o">.</span><span class="n">mktime</span><span class="p">(</span><span class="n">x</span><span class="p">))</span>
<span class="c1"># 字符串格式的日期</span>
<span class="nb">print</span><span class="p">(</span><span class="n">time</span><span class="o">.</span><span class="n">strftime</span><span class="p">(</span><span class="s1">&#39;%Y-%m-</span><span class="si">%d</span><span class="s1"> %H:%M:%S | %A | </span><span class="si">%a</span><span class="s1">&#39;</span><span class="p">,</span> <span class="n">time</span><span class="o">.</span><span class="n">localtime</span><span class="p">()))</span>
<span class="c1"># 字符串转换为tuple日期</span>
<span class="nb">print</span><span class="p">(</span><span class="n">time</span><span class="o">.</span><span class="n">strptime</span><span class="p">(</span><span class="s1">&#39;2018-02-13 17:10:23 | Tuesday | Tue&#39;</span><span class="p">,</span> <span class="s1">&#39;%Y-%m-</span><span class="si">%d</span><span class="s1"> %H:%M:%S | %A | </span><span class="si">%a</span><span class="s1">&#39;</span><span class="p">))</span>
<span class="c1"># tuple时间转字符串</span>
<span class="nb">print</span><span class="p">(</span><span class="n">time</span><span class="o">.</span><span class="n">asctime</span><span class="p">(</span><span class="n">x</span><span class="p">))</span>
<span class="c1"># 秒时间转字符串</span>
<span class="nb">print</span><span class="p">(</span><span class="n">time</span><span class="o">.</span><span class="n">ctime</span><span class="p">(</span><span class="n">time</span><span class="o">.</span><span class="n">time</span><span class="p">()))</span>
</code></pre></td></tr></table>
</div>
</div><div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span><span class="lnt">14
</span><span class="lnt">15
</span><span class="lnt">16
</span><span class="lnt">17
</span><span class="lnt">18
</span><span class="lnt">19
</span><span class="lnt">20
</span><span class="lnt">21
</span><span class="lnt">22
</span><span class="lnt">23
</span><span class="lnt">24
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-python" data-lang="python"><span class="kn">import</span> <span class="nn">datetime</span>
<span class="mf">1.</span><span class="n">返回当前时间</span>
<span class="o">&gt;&gt;&gt;</span> <span class="n">datetime</span><span class="o">.</span><span class="n">datetime</span><span class="o">.</span><span class="n">now</span><span class="p">()</span>
<span class="n">datetime</span><span class="o">.</span><span class="n">datetime</span><span class="p">(</span><span class="mi">2017</span><span class="p">,</span> <span class="mi">5</span><span class="p">,</span> <span class="mi">9</span><span class="p">,</span> <span class="mi">17</span><span class="p">,</span> <span class="mi">7</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">514481</span><span class="p">)</span>

<span class="mf">2.</span><span class="n">时间戳转换成日期</span>
<span class="o">&gt;&gt;&gt;</span> <span class="n">datetime</span><span class="o">.</span><span class="n">date</span><span class="o">.</span><span class="n">fromtimestamp</span><span class="p">(</span><span class="mi">1178766678</span><span class="p">)</span>
<span class="n">datetime</span><span class="o">.</span><span class="n">date</span><span class="p">(</span><span class="mi">2007</span><span class="p">,</span> <span class="mi">5</span><span class="p">,</span> <span class="mi">10</span><span class="p">)</span>

<span class="mf">3.</span><span class="n">当前时间</span><span class="o">+</span><span class="mi">3</span><span class="n">天</span>
<span class="o">&gt;&gt;&gt;</span> <span class="n">datetime</span><span class="o">.</span><span class="n">datetime</span><span class="o">.</span><span class="n">now</span><span class="p">()</span> <span class="o">+</span> <span class="n">datetime</span><span class="o">.</span><span class="n">timedelta</span><span class="p">(</span><span class="o">+</span><span class="mi">3</span><span class="p">)</span>
<span class="n">datetime</span><span class="o">.</span><span class="n">datetime</span><span class="p">(</span><span class="mi">2017</span><span class="p">,</span> <span class="mi">5</span><span class="p">,</span> <span class="mi">12</span><span class="p">,</span> <span class="mi">17</span><span class="p">,</span> <span class="mi">12</span><span class="p">,</span> <span class="mi">42</span><span class="p">,</span> <span class="mi">124379</span><span class="p">)</span>

<span class="mf">4.</span><span class="n">当前时间</span><span class="o">-</span><span class="mi">3</span><span class="n">天</span>
<span class="o">&gt;&gt;&gt;</span> <span class="n">datetime</span><span class="o">.</span><span class="n">datetime</span><span class="o">.</span><span class="n">now</span><span class="p">()</span> <span class="o">+</span> <span class="n">datetime</span><span class="o">.</span><span class="n">timedelta</span><span class="p">(</span><span class="o">-</span><span class="mi">3</span><span class="p">)</span>
<span class="n">datetime</span><span class="o">.</span><span class="n">datetime</span><span class="p">(</span><span class="mi">2017</span><span class="p">,</span> <span class="mi">5</span><span class="p">,</span> <span class="mi">6</span><span class="p">,</span> <span class="mi">17</span><span class="p">,</span> <span class="mi">13</span><span class="p">,</span> <span class="mi">18</span><span class="p">,</span> <span class="mi">474406</span><span class="p">)</span>

<span class="mf">5.</span><span class="n">当前时间</span><span class="o">+</span><span class="mi">3</span><span class="n">小时</span>
<span class="o">&gt;&gt;&gt;</span> <span class="n">datetime</span><span class="o">.</span><span class="n">datetime</span><span class="o">.</span><span class="n">now</span><span class="p">()</span> <span class="o">+</span> <span class="n">datetime</span><span class="o">.</span><span class="n">timedelta</span><span class="p">(</span><span class="n">hours</span><span class="o">=</span><span class="mi">3</span><span class="p">)</span>
<span class="n">datetime</span><span class="o">.</span><span class="n">datetime</span><span class="p">(</span><span class="mi">2017</span><span class="p">,</span> <span class="mi">5</span><span class="p">,</span> <span class="mi">9</span><span class="p">,</span> <span class="mi">20</span><span class="p">,</span> <span class="mi">13</span><span class="p">,</span> <span class="mi">55</span><span class="p">,</span> <span class="mi">678310</span><span class="p">)</span>

<span class="mf">6.</span><span class="n">当前时间</span><span class="o">+</span><span class="mi">30</span><span class="n">分钟</span>
<span class="o">&gt;&gt;&gt;</span> <span class="n">datetime</span><span class="o">.</span><span class="n">datetime</span><span class="o">.</span><span class="n">now</span><span class="p">()</span> <span class="o">+</span> <span class="n">datetime</span><span class="o">.</span><span class="n">timedelta</span><span class="p">(</span><span class="n">minutes</span><span class="o">=</span><span class="mi">30</span><span class="p">)</span>
<span class="n">datetime</span><span class="o">.</span><span class="n">datetime</span><span class="p">(</span><span class="mi">2017</span><span class="p">,</span> <span class="mi">5</span><span class="p">,</span> <span class="mi">9</span><span class="p">,</span> <span class="mi">17</span><span class="p">,</span> <span class="mi">44</span><span class="p">,</span> <span class="mi">40</span><span class="p">,</span> <span class="mi">392370</span><span class="p">)</span>
</code></pre></td></tr></table>
</div>
</div><h3 id="json模块">json模块</h3>
<blockquote>
<p>dump 或 dumps， 把其他对象或者格式，转化为 json 格式，dumps 用来处理字符串，dump 用来处理文件。
load 或 loads ，把 json 格式转化成其他格式，loads 用来处理字符串，load 用来处理文件。</p>
</blockquote>
<p><strong>json.dumps(data)</strong></p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span><span class="lnt">5
</span><span class="lnt">6
</span><span class="lnt">7
</span><span class="lnt">8
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-python" data-lang="python"><span class="kn">import</span> <span class="nn">json</span>

<span class="n">data</span> <span class="o">=</span> <span class="p">{</span><span class="s1">&#39;Citizen_Wang&#39;</span><span class="p">:</span><span class="mi">1</span><span class="p">,</span> <span class="s1">&#39;Always fall in love&#39;</span><span class="p">:</span><span class="mi">2</span><span class="p">,</span> <span class="s1">&#39;with neighbours.&#39;</span><span class="p">:</span><span class="mi">3</span><span class="p">}</span>
<span class="n">json_str</span> <span class="o">=</span> <span class="n">json</span><span class="o">.</span><span class="n">dumps</span><span class="p">(</span><span class="n">data</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="s1">&#39;Python 原始数据是：&#39;</span><span class="p">,</span> <span class="n">data</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="s1">&#39;json 对象：&#39;</span><span class="p">,</span><span class="n">json_str</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="nb">type</span><span class="p">(</span><span class="n">data</span><span class="p">))</span>
<span class="nb">print</span><span class="p">(</span><span class="nb">type</span><span class="p">(</span><span class="n">json_str</span><span class="p">))</span>
</code></pre></td></tr></table>
</div>
</div><p><strong>json.loads(str)</strong></p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span><span class="lnt">5
</span><span class="lnt">6
</span><span class="lnt">7
</span><span class="lnt">8
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-python" data-lang="python"><span class="kn">import</span> <span class="nn">json</span>

<span class="n">data</span> <span class="o">=</span> <span class="p">{</span><span class="s1">&#39;Citizen_Wang&#39;</span><span class="p">:</span><span class="mi">1</span><span class="p">,</span> <span class="s1">&#39;Always fall in love&#39;</span><span class="p">:</span><span class="mi">2</span><span class="p">,</span> <span class="s1">&#39;with neighbours.&#39;</span><span class="p">:</span><span class="mi">3</span><span class="p">}</span>
<span class="n">json_str</span> <span class="o">=</span> <span class="n">json</span><span class="o">.</span><span class="n">dumps</span><span class="p">(</span><span class="n">data</span><span class="p">)</span>

<span class="n">date2</span> <span class="o">=</span> <span class="n">json</span><span class="o">.</span><span class="n">loads</span><span class="p">(</span><span class="n">json_str</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="n">date2</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="nb">type</span><span class="p">(</span><span class="n">date2</span><span class="p">))</span>
</code></pre></td></tr></table>
</div>
</div><p><strong>dump()和load()</strong></p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-python" data-lang="python"><span class="kn">import</span> <span class="nn">json</span>

<span class="n">data</span> <span class="o">=</span> <span class="p">{</span><span class="s1">&#39;Citizen_Wang&#39;</span><span class="p">:</span><span class="mi">1</span><span class="p">,</span> <span class="s1">&#39;Always fall in love&#39;</span><span class="p">:</span><span class="mi">2</span><span class="p">,</span> <span class="s1">&#39;with neighbours.&#39;</span><span class="p">:</span><span class="mi">3</span><span class="p">}</span>
<span class="n">json_str</span> <span class="o">=</span> <span class="n">json</span><span class="o">.</span><span class="n">dumps</span><span class="p">(</span><span class="n">data</span><span class="p">)</span>
<span class="n">file</span> <span class="o">=</span> <span class="nb">open</span><span class="p">(</span><span class="s1">&#39;2.txt&#39;</span><span class="p">,</span> <span class="s1">&#39;w&#39;</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="nb">type</span><span class="p">(</span><span class="n">file</span><span class="p">))</span>

<span class="n">json</span><span class="o">.</span><span class="n">dump</span><span class="p">(</span><span class="n">data</span><span class="p">,</span> <span class="n">file</span><span class="p">)</span>
<span class="n">json</span><span class="o">.</span><span class="n">dump</span><span class="p">(</span><span class="n">json_str</span><span class="p">,</span> <span class="n">file</span><span class="p">)</span>
<span class="n">file</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>

<span class="n">file</span> <span class="o">=</span> <span class="nb">open</span><span class="p">(</span><span class="s1">&#39;2.txt&#39;</span><span class="p">,</span> <span class="s1">&#39;r&#39;</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="n">json</span><span class="o">.</span><span class="n">load</span><span class="p">(</span><span class="n">file</span><span class="p">))</span>
</code></pre></td></tr></table>
</div>
</div><h3 id="copy模块">copy模块</h3>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span><span class="lnt">14
</span><span class="lnt">15
</span><span class="lnt">16
</span><span class="lnt">17
</span><span class="lnt">18
</span><span class="lnt">19
</span><span class="lnt">20
</span><span class="lnt">21
</span><span class="lnt">22
</span><span class="lnt">23
</span><span class="lnt">24
</span><span class="lnt">25
</span><span class="lnt">26
</span><span class="lnt">27
</span><span class="lnt">28
</span><span class="lnt">29
</span><span class="lnt">30
</span><span class="lnt">31
</span><span class="lnt">32
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-python" data-lang="python"><span class="c1">#深拷贝</span>

<span class="c1">#浅拷贝</span>
<span class="c1">#1 等号</span>
<span class="c1"># list1=[11,22,33,[&#39;a&#39;,&#39;b&#39;]]</span>
<span class="c1"># list2=list1</span>
<span class="c1"># list1.append(44)</span>
<span class="c1"># print(list1)</span>
<span class="c1"># print(list2)</span>

<span class="c1">#2 copy</span>
<span class="kn">import</span> <span class="nn">copy</span>
<span class="c1"># c=[11,22,33,[&#39;a&#39;,&#39;b&#39;]]</span>
<span class="c1"># d=copy.copy(c)</span>
<span class="c1"># c.append(44)</span>
<span class="c1"># print(c)</span>
<span class="c1"># print(d)#d不受影响,要会根据栈、堆分析</span>
<span class="c1">#</span>
<span class="c1"># c=[11,22,33,[&#39;a&#39;,&#39;b&#39;]]</span>
<span class="c1"># d=copy.copy(c)</span>
<span class="c1"># c[3].append(&#39;c&#39;)</span>
<span class="c1"># print(c)#[11, 22, 33, [&#39;a&#39;, &#39;b&#39;, &#39;c&#39;]]</span>
<span class="c1"># print(d)#[11, 22, 33, [&#39;a&#39;, &#39;b&#39;, &#39;c&#39;]]</span>



<span class="c1">#深拷贝</span>
<span class="n">c</span><span class="o">=</span><span class="p">[</span><span class="mi">11</span><span class="p">,</span><span class="mi">22</span><span class="p">,</span><span class="mi">33</span><span class="p">,[</span><span class="s1">&#39;a&#39;</span><span class="p">,</span><span class="s1">&#39;b&#39;</span><span class="p">]]</span>
<span class="n">d</span><span class="o">=</span><span class="n">copy</span><span class="o">.</span><span class="n">deepcopy</span><span class="p">(</span><span class="n">c</span><span class="p">)</span>
<span class="n">c</span><span class="p">[</span><span class="mi">3</span><span class="p">]</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="s1">&#39;c&#39;</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="n">c</span><span class="p">)</span><span class="c1">#[11, 22, 33, [&#39;a&#39;, &#39;b&#39;, &#39;c&#39;]]</span>
<span class="nb">print</span><span class="p">(</span><span class="n">d</span><span class="p">)</span><span class="c1">#[11, 22, 33, [&#39;a&#39;, &#39;b&#39;]]</span>
</code></pre></td></tr></table>
</div>
</div><h3 id="operator操作符">operator操作符</h3>
<p>operator 模块是 Python 中内置的操作符函数接口，它定义了算术，比较和与标准对象 API 相对应的其他操作的内置函数。
operator 模块是用 C 实现的，所以执行速度比 Python 代码快。</p>
<h4 id="逻辑运算">逻辑运算</h4>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-python" data-lang="python"><span class="kn">from</span> <span class="nn">operator</span> <span class="kn">import</span> <span class="o">*</span>

<span class="n">a</span> <span class="o">=</span> <span class="o">-</span><span class="mi">1</span>
<span class="n">b</span> <span class="o">=</span> <span class="mi">5</span>

<span class="nb">print</span><span class="p">(</span><span class="s1">&#39;a =&#39;</span><span class="p">,</span> <span class="n">a</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="s1">&#39;b =&#39;</span><span class="p">,</span> <span class="n">b</span><span class="p">)</span>
<span class="nb">print</span><span class="p">()</span>

<span class="nb">print</span><span class="p">(</span><span class="s1">&#39;not_(a)     :&#39;</span><span class="p">,</span> <span class="n">not_</span><span class="p">(</span><span class="n">a</span><span class="p">))</span>       <span class="c1"># False</span>
<span class="nb">print</span><span class="p">(</span><span class="s1">&#39;truth(a)    :&#39;</span><span class="p">,</span> <span class="n">truth</span><span class="p">(</span><span class="n">a</span><span class="p">))</span>      <span class="c1"># True        </span>
<span class="nb">print</span><span class="p">(</span><span class="s1">&#39;is_(a, b)   :&#39;</span><span class="p">,</span> <span class="n">is_</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">))</span>     <span class="c1"># False</span>
<span class="nb">print</span><span class="p">(</span><span class="s1">&#39;is_not(a, b):&#39;</span><span class="p">,</span> <span class="n">is_not</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">))</span>  <span class="c1"># True</span>
</code></pre></td></tr></table>
</div>
</div><p>####　比较运算符</p>
<p>功能是等同于使用表达式语法&lt;， &lt;=，==，&gt;=，和&gt;。</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span><span class="lnt">14
</span><span class="lnt">15
</span><span class="lnt">16
</span><span class="lnt">17
</span><span class="lnt">18
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-python" data-lang="python"><span class="kn">from</span> <span class="nn">operator</span> <span class="kn">import</span> <span class="o">*</span>

<span class="n">a</span> <span class="o">=</span> <span class="mi">1</span>
<span class="n">b</span> <span class="o">=</span> <span class="mf">5.0</span>

<span class="nb">print</span><span class="p">(</span><span class="s1">&#39;a =&#39;</span><span class="p">,</span> <span class="n">a</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="s1">&#39;b =&#39;</span><span class="p">,</span> <span class="n">b</span><span class="p">)</span>
<span class="k">for</span> <span class="n">func</span> <span class="ow">in</span> <span class="p">(</span><span class="n">lt</span><span class="p">,</span> <span class="n">le</span><span class="p">,</span> <span class="n">eq</span><span class="p">,</span> <span class="n">ne</span><span class="p">,</span> <span class="n">ge</span><span class="p">,</span> <span class="n">gt</span><span class="p">):</span>
    <span class="nb">print</span><span class="p">(</span><span class="s1">&#39;</span><span class="si">{}</span><span class="s1">(a, b): </span><span class="si">{}</span><span class="s1">&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">func</span><span class="o">.</span><span class="vm">__name__</span><span class="p">,</span> <span class="n">func</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">)))</span>
    
<span class="c1"># a = 1</span>
<span class="c1"># b = 5.0</span>
<span class="c1"># lt(a, b): True</span>
<span class="c1"># le(a, b): True</span>
<span class="c1"># eq(a, b): False</span>
<span class="c1"># ne(a, b): True</span>
<span class="c1"># ge(a, b): False</span>
<span class="c1"># gt(a, b): False    </span>
</code></pre></td></tr></table>
</div>
</div><h4 id="算术运算符">算术运算符</h4>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span><span class="lnt">14
</span><span class="lnt">15
</span><span class="lnt">16
</span><span class="lnt">17
</span><span class="lnt">18
</span><span class="lnt">19
</span><span class="lnt">20
</span><span class="lnt">21
</span><span class="lnt">22
</span><span class="lnt">23
</span><span class="lnt">24
</span><span class="lnt">25
</span><span class="lnt">26
</span><span class="lnt">27
</span><span class="lnt">28
</span><span class="lnt">29
</span><span class="lnt">30
</span><span class="lnt">31
</span><span class="lnt">32
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-python" data-lang="python"><span class="kn">from</span> <span class="nn">operator</span> <span class="kn">import</span> <span class="o">*</span>

<span class="n">a</span> <span class="o">=</span> <span class="o">-</span><span class="mi">1</span>
<span class="n">b</span> <span class="o">=</span> <span class="mf">5.0</span>
<span class="n">c</span> <span class="o">=</span> <span class="mi">2</span>
<span class="n">d</span> <span class="o">=</span> <span class="mi">6</span>

<span class="nb">print</span><span class="p">(</span><span class="s1">&#39;</span><span class="se">\n</span><span class="s1">Positive/Negative:&#39;</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="s1">&#39;abs(a):&#39;</span><span class="p">,</span> <span class="nb">abs</span><span class="p">(</span><span class="n">a</span><span class="p">))</span>    <span class="c1"># abs(a): 1</span>
<span class="nb">print</span><span class="p">(</span><span class="s1">&#39;neg(a):&#39;</span><span class="p">,</span> <span class="n">neg</span><span class="p">(</span><span class="n">a</span><span class="p">))</span>    <span class="c1"># neg(a): 1</span>
<span class="nb">print</span><span class="p">(</span><span class="s1">&#39;neg(b):&#39;</span><span class="p">,</span> <span class="n">neg</span><span class="p">(</span><span class="n">b</span><span class="p">))</span>    <span class="c1"># neg(b): -5.0</span>
<span class="nb">print</span><span class="p">(</span><span class="s1">&#39;pos(a):&#39;</span><span class="p">,</span> <span class="n">pos</span><span class="p">(</span><span class="n">a</span><span class="p">))</span>    <span class="c1"># pos(a): -1</span>
<span class="nb">print</span><span class="p">(</span><span class="s1">&#39;pos(b):&#39;</span><span class="p">,</span> <span class="n">pos</span><span class="p">(</span><span class="n">b</span><span class="p">))</span>    <span class="c1"># pos(b): 5.0</span>

<span class="nb">print</span><span class="p">(</span><span class="s1">&#39;</span><span class="se">\n</span><span class="s1">Arithmetic:&#39;</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="s1">&#39;add(a, b)     :&#39;</span><span class="p">,</span> <span class="n">add</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">))</span>         <span class="c1"># add(a, b)     : 4.0</span>
<span class="nb">print</span><span class="p">(</span><span class="s1">&#39;floordiv(a, b):&#39;</span><span class="p">,</span> <span class="n">floordiv</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">))</span>    <span class="c1"># floordiv(a, b): -1.0</span>
<span class="nb">print</span><span class="p">(</span><span class="s1">&#39;floordiv(d, c):&#39;</span><span class="p">,</span> <span class="n">floordiv</span><span class="p">(</span><span class="n">d</span><span class="p">,</span> <span class="n">c</span><span class="p">))</span>    <span class="c1"># floordiv(d, c): 3</span>
<span class="nb">print</span><span class="p">(</span><span class="s1">&#39;mod(a, b)     :&#39;</span><span class="p">,</span> <span class="n">mod</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">))</span>         <span class="c1"># mod(a, b)     : 4.0</span>
<span class="nb">print</span><span class="p">(</span><span class="s1">&#39;mul(a, b)     :&#39;</span><span class="p">,</span> <span class="n">mul</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">))</span>         <span class="c1"># mul(a, b)     : -5.0</span>
<span class="nb">print</span><span class="p">(</span><span class="s1">&#39;pow(c, d)     :&#39;</span><span class="p">,</span> <span class="nb">pow</span><span class="p">(</span><span class="n">c</span><span class="p">,</span> <span class="n">d</span><span class="p">))</span>         <span class="c1"># pow(c, d)     : 64</span>
<span class="nb">print</span><span class="p">(</span><span class="s1">&#39;sub(b, a)     :&#39;</span><span class="p">,</span> <span class="n">sub</span><span class="p">(</span><span class="n">b</span><span class="p">,</span> <span class="n">a</span><span class="p">))</span>         <span class="c1"># sub(b, a)     : 6.0</span>
<span class="nb">print</span><span class="p">(</span><span class="s1">&#39;truediv(a, b) :&#39;</span><span class="p">,</span> <span class="n">truediv</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">))</span>     <span class="c1"># truediv(a, b) : -0.2</span>
<span class="nb">print</span><span class="p">(</span><span class="s1">&#39;truediv(d, c) :&#39;</span><span class="p">,</span> <span class="n">truediv</span><span class="p">(</span><span class="n">d</span><span class="p">,</span> <span class="n">c</span><span class="p">))</span>     <span class="c1"># truediv(d, c) : 3.0</span>

<span class="nb">print</span><span class="p">(</span><span class="s1">&#39;</span><span class="se">\n</span><span class="s1">Bitwise:&#39;</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="s1">&#39;and_(c, d)  :&#39;</span><span class="p">,</span> <span class="n">and_</span><span class="p">(</span><span class="n">c</span><span class="p">,</span> <span class="n">d</span><span class="p">))</span>      <span class="c1"># and_(c, d)  : 2       </span>
<span class="nb">print</span><span class="p">(</span><span class="s1">&#39;invert(c)   :&#39;</span><span class="p">,</span> <span class="n">invert</span><span class="p">(</span><span class="n">c</span><span class="p">))</span>       <span class="c1"># invert(c)   : -3</span>
<span class="nb">print</span><span class="p">(</span><span class="s1">&#39;lshift(c, d):&#39;</span><span class="p">,</span> <span class="n">lshift</span><span class="p">(</span><span class="n">c</span><span class="p">,</span> <span class="n">d</span><span class="p">))</span>    <span class="c1"># lshift(c, d): 128</span>
<span class="nb">print</span><span class="p">(</span><span class="s1">&#39;or_(c, d)   :&#39;</span><span class="p">,</span> <span class="n">or_</span><span class="p">(</span><span class="n">c</span><span class="p">,</span> <span class="n">d</span><span class="p">))</span>       <span class="c1"># or_(c, d)   : 6</span>
<span class="nb">print</span><span class="p">(</span><span class="s1">&#39;rshift(d, c):&#39;</span><span class="p">,</span> <span class="n">rshift</span><span class="p">(</span><span class="n">d</span><span class="p">,</span> <span class="n">c</span><span class="p">))</span>    <span class="c1"># rshift(d, c): 1</span>
<span class="nb">print</span><span class="p">(</span><span class="s1">&#39;xor(c, d)   :&#39;</span><span class="p">,</span> <span class="n">xor</span><span class="p">(</span><span class="n">c</span><span class="p">,</span> <span class="n">d</span><span class="p">))</span>       <span class="c1"># xor(c, d)   : 4</span>
</code></pre></td></tr></table>
</div>
</div><h4 id="序列运算符">序列运算符</h4>
<p>使用序列的运算符可以分为四组：构建序列，搜索项目，访问内容以及从序列中删除项目。
其中一些操作（例如setitem()和delitem()）修改了序列并且不返回值。</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span><span class="lnt">14
</span><span class="lnt">15
</span><span class="lnt">16
</span><span class="lnt">17
</span><span class="lnt">18
</span><span class="lnt">19
</span><span class="lnt">20
</span><span class="lnt">21
</span><span class="lnt">22
</span><span class="lnt">23
</span><span class="lnt">24
</span><span class="lnt">25
</span><span class="lnt">26
</span><span class="lnt">27
</span><span class="lnt">28
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-python" data-lang="python"><span class="kn">from</span> <span class="nn">operator</span> <span class="kn">import</span> <span class="o">*</span>

<span class="n">a</span> <span class="o">=</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">]</span>
<span class="n">b</span> <span class="o">=</span> <span class="p">[</span><span class="s1">&#39;a&#39;</span><span class="p">,</span> <span class="s1">&#39;b&#39;</span><span class="p">,</span> <span class="s1">&#39;c&#39;</span><span class="p">]</span>

<span class="nb">print</span><span class="p">(</span><span class="s1">&#39;</span><span class="se">\n</span><span class="s1">Constructive:&#39;</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="s1">&#39;  concat(a, b):&#39;</span><span class="p">,</span> <span class="n">concat</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">))</span>      <span class="c1"># concat(a, b): [1, 2, 3, &#39;a&#39;, &#39;b&#39;, &#39;c&#39;]</span>

<span class="nb">print</span><span class="p">(</span><span class="s1">&#39;</span><span class="se">\n</span><span class="s1">Searching:&#39;</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="s1">&#39;  contains(a, 1)  :&#39;</span><span class="p">,</span> <span class="n">contains</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="mi">1</span><span class="p">))</span>    <span class="c1"># contains(a, 1)  : True</span>
<span class="nb">print</span><span class="p">(</span><span class="s1">&#39;  contains(b, &#34;d&#34;):&#39;</span><span class="p">,</span> <span class="n">contains</span><span class="p">(</span><span class="n">b</span><span class="p">,</span> <span class="s2">&#34;d&#34;</span><span class="p">))</span>  <span class="c1"># contains(b, &#34;d&#34;): False</span>
<span class="nb">print</span><span class="p">(</span><span class="s1">&#39;  countOf(a, 1)   :&#39;</span><span class="p">,</span> <span class="n">countOf</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="mi">1</span><span class="p">))</span>     <span class="c1"># countOf(a, 1)   : 1</span>
<span class="nb">print</span><span class="p">(</span><span class="s1">&#39;  countOf(b, &#34;d&#34;) :&#39;</span><span class="p">,</span> <span class="n">countOf</span><span class="p">(</span><span class="n">b</span><span class="p">,</span> <span class="s2">&#34;d&#34;</span><span class="p">))</span>   <span class="c1"># countOf(b, &#34;d&#34;) : 0</span>
<span class="nb">print</span><span class="p">(</span><span class="s1">&#39;  indexOf(a, 5)   :&#39;</span><span class="p">,</span> <span class="n">indexOf</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="mi">1</span><span class="p">))</span>     <span class="c1"># indexOf(a, 5)   : 0</span>

<span class="nb">print</span><span class="p">(</span><span class="s1">&#39;</span><span class="se">\n</span><span class="s1">Access Items:&#39;</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="n">getitem</span><span class="p">(</span><span class="n">b</span><span class="p">,</span> <span class="mi">1</span><span class="p">))</span>    <span class="c1"># b</span>
<span class="nb">print</span><span class="p">(</span><span class="n">getitem</span><span class="p">(</span><span class="n">b</span><span class="p">,</span> <span class="nb">slice</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">3</span><span class="p">)))</span> <span class="c1"># [&#39;b&#39;, &#39;c&#39;]</span>
<span class="nb">print</span><span class="p">(</span><span class="n">setitem</span><span class="p">(</span><span class="n">b</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="s2">&#34;d&#34;</span><span class="p">)</span>    <span class="c1"># None</span>
<span class="nb">print</span><span class="p">(</span><span class="n">b</span><span class="p">)</span>    <span class="c1"># [&#39;a&#39;, &#39;d&#39;, &#39;c&#39;]</span>
<span class="nb">print</span><span class="p">(</span><span class="n">setitem</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="nb">slice</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">3</span><span class="p">),</span> <span class="p">[</span><span class="mi">4</span><span class="p">,</span> <span class="mi">5</span><span class="p">])</span><span class="err">）</span>  <span class="c1"># None</span>
<span class="nb">print</span><span class="p">(</span><span class="n">a</span><span class="p">)</span>    <span class="c1"># [1, 4, 5]</span>

<span class="nb">print</span><span class="p">(</span><span class="s1">&#39;</span><span class="se">\n</span><span class="s1">Destructive:&#39;</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="n">delitem</span><span class="p">(</span><span class="n">b</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span><span class="err">）</span>    <span class="c1"># None</span>
<span class="nb">print</span><span class="p">(</span><span class="n">b</span><span class="p">)</span>    <span class="c1"># [&#39;a&#39;, &#39;c&#39;]</span>
<span class="nb">print</span><span class="p">(</span><span class="n">delitem</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="nb">slice</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">3</span><span class="p">))</span>   <span class="c1"># None</span>
<span class="nb">print</span><span class="p">(</span><span class="n">a</span><span class="p">)</span>    <span class="c1"># [1]</span>
</code></pre></td></tr></table>
</div>
</div><h4 id="原地操作符">原地操作符</h4>
<p>除了标准运算符之外，许多类型的对象还支持通过特殊运算符进行“原地”修改 ，+=同样具有就地修改的功能：</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-python" data-lang="python"><span class="kn">from</span> <span class="nn">operator</span> <span class="kn">import</span> <span class="o">*</span>

<span class="n">a</span> <span class="o">=</span> <span class="o">-</span><span class="mi">1</span>
<span class="n">b</span> <span class="o">=</span> <span class="mf">5.0</span>
<span class="n">c</span> <span class="o">=</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">]</span>
<span class="n">d</span> <span class="o">=</span> <span class="p">[</span><span class="s1">&#39;a&#39;</span><span class="p">,</span> <span class="s1">&#39;b&#39;</span><span class="p">,</span> <span class="s1">&#39;c&#39;</span><span class="p">]</span>

<span class="n">a</span> <span class="o">=</span> <span class="n">iadd</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="s1">&#39;a = iadd(a, b) =&gt;&#39;</span><span class="p">,</span> <span class="n">a</span><span class="p">)</span>   <span class="c1"># a = iadd(a, b) =&gt; 4.0</span>

<span class="n">c</span> <span class="o">=</span> <span class="n">iconcat</span><span class="p">(</span><span class="n">c</span><span class="p">,</span> <span class="n">d</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="s1">&#39;c = iconcat(c, d) =&gt;&#39;</span><span class="p">,</span> <span class="n">c</span><span class="p">)</span>    <span class="c1"># c = iconcat(c, d) =&gt; [1, 2, 3, &#39;a&#39;, &#39;b&#39;, &#39;c&#39;]</span>
</code></pre></td></tr></table>
</div>
</div><h4 id="属性和元素的获取方法">属性和元素的获取方法</h4>
<p>operator 模块最特别的特性之一就是获取方法的概念，获取方法是运行时构造的一些可回调对象，用来获取对象的属性或序列的内容，获取方法在处理迭代器或生成器序列的时候特别有用，它们引入的开销会大大降低 lambda 或 Python 函数的开销。</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span><span class="lnt">14
</span><span class="lnt">15
</span><span class="lnt">16
</span><span class="lnt">17
</span><span class="lnt">18
</span><span class="lnt">19
</span><span class="lnt">20
</span><span class="lnt">21
</span><span class="lnt">22
</span><span class="lnt">23
</span><span class="lnt">24
</span><span class="lnt">25
</span><span class="lnt">26
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-python" data-lang="python"><span class="kn">from</span> <span class="nn">operator</span> <span class="kn">import</span> <span class="o">*</span>


<span class="k">class</span> <span class="nc">MyObj</span><span class="p">:</span>
    <span class="s2">&#34;&#34;&#34;example class for attrgetter&#34;&#34;&#34;</span>

    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">arg</span><span class="p">):</span>
        <span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="fm">__init__</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">arg</span> <span class="o">=</span> <span class="n">arg</span>

    <span class="k">def</span> <span class="fm">__repr__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="s1">&#39;MyObj(</span><span class="si">{}</span><span class="s1">)&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">arg</span><span class="p">)</span>


<span class="n">l</span> <span class="o">=</span> <span class="p">[</span><span class="n">MyObj</span><span class="p">(</span><span class="n">i</span><span class="p">)</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">5</span><span class="p">)]</span>
<span class="nb">print</span><span class="p">(</span><span class="n">l</span><span class="p">)</span>    <span class="c1"># [MyObj(0), MyObj(1), MyObj(2), MyObj(3), MyObj(4)]</span>

<span class="c1"># Extract the &#39;arg&#39; value from each object</span>
<span class="n">g</span> <span class="o">=</span> <span class="n">attrgetter</span><span class="p">(</span><span class="s1">&#39;arg&#39;</span><span class="p">)</span>
<span class="n">vals</span> <span class="o">=</span> <span class="p">[</span><span class="n">g</span><span class="p">(</span><span class="n">i</span><span class="p">)</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="n">l</span><span class="p">]</span>
<span class="nb">print</span><span class="p">(</span><span class="s1">&#39;arg values:&#39;</span><span class="p">,</span> <span class="n">vals</span><span class="p">)</span>  <span class="c1"># arg values: [0, 1, 2, 3, 4]</span>

<span class="c1"># Sort using arg</span>
<span class="n">l</span><span class="o">.</span><span class="n">reverse</span><span class="p">()</span>
<span class="nb">print</span><span class="p">(</span><span class="n">l</span><span class="p">)</span>    <span class="c1"># [MyObj(4), MyObj(3), MyObj(2), MyObj(1), MyObj(0)]</span>
<span class="nb">print</span><span class="p">(</span><span class="nb">sorted</span><span class="p">(</span><span class="n">l</span><span class="p">,</span> <span class="n">key</span><span class="o">=</span><span class="n">g</span><span class="p">))</span> <span class="c1"># [MyObj(0), MyObj(1), MyObj(2), MyObj(3), MyObj(4)]</span>
</code></pre></td></tr></table>
</div>
</div><h4 id="结合操作符和定制类">结合操作符和定制类</h4>
<p>operator模块中的函数通过标准 Python 接口进行操作，因此它可以使用用户定义的类以及内置类型。</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span><span class="lnt">14
</span><span class="lnt">15
</span><span class="lnt">16
</span><span class="lnt">17
</span><span class="lnt">18
</span><span class="lnt">19
</span><span class="lnt">20
</span><span class="lnt">21
</span><span class="lnt">22
</span><span class="lnt">23
</span><span class="lnt">24
</span><span class="lnt">25
</span><span class="lnt">26
</span><span class="lnt">27
</span><span class="lnt">28
</span><span class="lnt">29
</span><span class="lnt">30
</span><span class="lnt">31
</span><span class="lnt">32
</span><span class="lnt">33
</span><span class="lnt">34
</span><span class="lnt">35
</span><span class="lnt">36
</span><span class="lnt">37
</span><span class="lnt">38
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-python" data-lang="python"><span class="kn">from</span> <span class="nn">operator</span> <span class="kn">import</span> <span class="o">*</span>


<span class="k">class</span> <span class="nc">MyObj</span><span class="p">:</span>
    <span class="s2">&#34;&#34;&#34;Example for operator overloading&#34;&#34;&#34;</span>

    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">val</span><span class="p">):</span>
        <span class="nb">super</span><span class="p">(</span><span class="n">MyObj</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="fm">__init__</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">val</span> <span class="o">=</span> <span class="n">val</span>

    <span class="k">def</span> <span class="fm">__str__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="s1">&#39;MyObj(</span><span class="si">{}</span><span class="s1">)&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">val</span><span class="p">)</span>

    <span class="k">def</span> <span class="fm">__lt__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">other</span><span class="p">):</span>
        <span class="s2">&#34;&#34;&#34;compare for less-than&#34;&#34;&#34;</span>
        <span class="nb">print</span><span class="p">(</span><span class="s1">&#39;Testing </span><span class="si">{}</span><span class="s1"> &lt; </span><span class="si">{}</span><span class="s1">&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">other</span><span class="p">))</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">val</span> <span class="o">&lt;</span> <span class="n">other</span><span class="o">.</span><span class="n">val</span>

    <span class="k">def</span> <span class="fm">__add__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">other</span><span class="p">):</span>
        <span class="s2">&#34;&#34;&#34;add values&#34;&#34;&#34;</span>
        <span class="nb">print</span><span class="p">(</span><span class="s1">&#39;Adding </span><span class="si">{}</span><span class="s1"> + </span><span class="si">{}</span><span class="s1">&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">other</span><span class="p">))</span>
        <span class="k">return</span> <span class="n">MyObj</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">val</span> <span class="o">+</span> <span class="n">other</span><span class="o">.</span><span class="n">val</span><span class="p">)</span>


<span class="n">a</span> <span class="o">=</span> <span class="n">MyObj</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span>
<span class="n">b</span> <span class="o">=</span> <span class="n">MyObj</span><span class="p">(</span><span class="mi">2</span><span class="p">)</span>

<span class="nb">print</span><span class="p">(</span><span class="s1">&#39;Comparison:&#39;</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="n">lt</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">))</span>     
<span class="c1"># Comparison:</span>
<span class="c1"># Testing MyObj(1) &lt; MyObj(2)</span>
<span class="c1"># True</span>

<span class="nb">print</span><span class="p">(</span><span class="s1">&#39;</span><span class="se">\n</span><span class="s1">Arithmetic:&#39;</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="n">add</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">))</span>    
<span class="c1"># Arithmetic:</span>
<span class="c1"># Adding MyObj(1) + MyObj(2)</span>
<span class="c1"># MyObj(3)</span>
</code></pre></td></tr></table>
</div>
</div><h4 id="类型检查">类型检查</h4>
<p>operator 模块还包含一些函数用来测试映射、数字和序列类型的 API 兼容性。</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span><span class="lnt">14
</span><span class="lnt">15
</span><span class="lnt">16
</span><span class="lnt">17
</span><span class="lnt">18
</span><span class="lnt">19
</span><span class="lnt">20
</span><span class="lnt">21
</span><span class="lnt">22
</span><span class="lnt">23
</span><span class="lnt">24
</span><span class="lnt">25
</span><span class="lnt">26
</span><span class="lnt">27
</span><span class="lnt">28
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-python" data-lang="python"><span class="kn">from</span> <span class="nn">operator</span> <span class="kn">import</span> <span class="o">*</span>

<span class="k">class</span> <span class="nc">NoType</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
    <span class="k">pass</span>

<span class="k">class</span> <span class="nc">MultiType</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
    <span class="k">def</span> <span class="fm">__len__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="mi">0</span>

    <span class="k">def</span> <span class="fm">__getitem__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">name</span><span class="p">):</span>
        <span class="k">return</span> <span class="s2">&#34;mapping&#34;</span>

    <span class="k">def</span> <span class="fm">__int__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="mi">0</span>

<span class="n">o</span> <span class="o">=</span> <span class="n">NoType</span><span class="p">()</span>
<span class="n">t</span> <span class="o">=</span> <span class="n">MultiType</span><span class="p">()</span>

<span class="k">for</span> <span class="n">func</span> <span class="ow">in</span> <span class="p">[</span><span class="n">isMappingType</span><span class="p">,</span> <span class="n">isNumberType</span><span class="p">,</span> <span class="n">isSequenceType</span><span class="p">]:</span>
    <span class="nb">print</span> <span class="s2">&#34;</span><span class="si">%s</span><span class="s2">(o):&#34;</span> <span class="o">%</span> <span class="n">func</span><span class="o">.</span><span class="vm">__name__</span><span class="p">,</span> <span class="n">func</span><span class="p">(</span><span class="n">o</span><span class="p">)</span>
    <span class="nb">print</span> <span class="s2">&#34;</span><span class="si">%s</span><span class="s2">(t):&#34;</span> <span class="o">%</span> <span class="n">func</span><span class="o">.</span><span class="vm">__name__</span><span class="p">,</span> <span class="n">func</span><span class="p">(</span><span class="n">t</span><span class="p">)</span>

<span class="c1"># isMappingType(o): False</span>
<span class="c1"># isMappingType(t): True</span>
<span class="c1"># isNumberType(o): False</span>
<span class="c1"># isNumberType(t): True</span>
<span class="c1"># isSequenceType(o): False</span>
<span class="c1"># isSequenceType(t): True    </span>
</code></pre></td></tr></table>
</div>
</div><h4 id="获取对象方法">获取对象方法</h4>
<p>使用 methodcaller 可以获取对象的方法。</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-python" data-lang="python"><span class="kn">from</span> <span class="nn">operator</span> <span class="kn">import</span> <span class="n">methodcaller</span>

<span class="k">class</span> <span class="nc">Student</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">name</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">name</span> <span class="o">=</span> <span class="n">name</span>

    <span class="k">def</span> <span class="nf">getName</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">name</span>

<span class="n">stu</span> <span class="o">=</span> <span class="n">Student</span><span class="p">(</span><span class="s2">&#34;Jim&#34;</span><span class="p">)</span>
<span class="n">func</span> <span class="o">=</span> <span class="n">methodcaller</span><span class="p">(</span><span class="s1">&#39;getName&#39;</span><span class="p">)</span>
<span class="nb">print</span> <span class="n">func</span><span class="p">(</span><span class="n">stu</span><span class="p">)</span>   <span class="c1"># 输出Jim</span>
</code></pre></td></tr></table>
</div>
</div><p>还可以给方法传递参数：</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-python" data-lang="python"><span class="n">f</span> <span class="o">=</span> <span class="n">methodcaller</span><span class="p">(</span><span class="s1">&#39;name&#39;</span><span class="p">,</span> <span class="s1">&#39;foo&#39;</span><span class="p">,</span> <span class="n">bar</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span>
<span class="n">f</span><span class="p">(</span><span class="n">b</span><span class="p">)</span>    <span class="c1"># return   b.name(&#39;foo&#39;, bar=1)</span>
</code></pre></td></tr></table>
</div>
</div><p>methodcaller方法等价于下面这个函数：</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-python" data-lang="python"><span class="k">def</span> <span class="nf">methodcaller</span><span class="p">(</span><span class="n">name</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span>  <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
      <span class="k">def</span> <span class="nf">caller</span><span class="p">(</span><span class="n">obj</span><span class="p">):</span>
            <span class="k">return</span> <span class="nb">getattr</span><span class="p">(</span><span class="n">obj</span><span class="p">,</span> <span class="n">name</span><span class="p">)(</span><span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
      <span class="k">return</span> <span class="n">caller</span>
</code></pre></td></tr></table>
</div>
</div><h3 id="collections模块">collections模块</h3>
<h4 id="1-counter">1. Counter</h4>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span><span class="lnt">5
</span><span class="lnt">6
</span><span class="lnt">7
</span><span class="lnt">8
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-python" data-lang="python"><span class="kn">from</span> <span class="nn">collections</span> <span class="kn">import</span> <span class="n">Counter</span>
<span class="n">s</span> <span class="o">=</span> <span class="s1">&#39;abcbcaccbbad&#39;</span>  
<span class="n">l</span> <span class="o">=</span> <span class="p">[</span><span class="s1">&#39;a&#39;</span><span class="p">,</span><span class="s1">&#39;b&#39;</span><span class="p">,</span><span class="s1">&#39;c&#39;</span><span class="p">,</span><span class="s1">&#39;c&#39;</span><span class="p">,</span><span class="s1">&#39;a&#39;</span><span class="p">,</span><span class="s1">&#39;b&#39;</span><span class="p">,</span><span class="s1">&#39;b&#39;</span><span class="p">]</span>  
<span class="n">d</span> <span class="o">=</span> <span class="p">{</span><span class="s1">&#39;2&#39;</span><span class="p">:</span> <span class="mi">3</span><span class="p">,</span> <span class="s1">&#39;3&#39;</span><span class="p">:</span> <span class="mi">2</span><span class="p">,</span> <span class="s1">&#39;17&#39;</span><span class="p">:</span> <span class="mi">2</span><span class="p">}</span>  
<span class="c1"># Counter 获取各元素的个数,返回字典  </span>
<span class="nb">print</span><span class="p">(</span><span class="n">Counter</span><span class="p">(</span><span class="n">s</span><span class="p">))</span>   <span class="c1"># Counter({&#39;c&#39;: 4, &#39;b&#39;: 4, &#39;a&#39;: 3})  </span>
<span class="nb">print</span><span class="p">(</span><span class="n">Counter</span><span class="p">(</span><span class="n">l</span><span class="p">))</span>   <span class="c1"># Counter({&#39;b&#39;: 3, &#39;a&#39;: 2, &#39;c&#39;: 2})  </span>
<span class="nb">print</span><span class="p">(</span><span class="n">Counter</span><span class="p">(</span><span class="n">d</span><span class="p">))</span>   <span class="c1"># Counter({3: 3, 2: 2, 17: 1})  </span>
</code></pre></td></tr></table>
</div>
</div><p><strong>1. most_common</strong></p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-python" data-lang="python"><span class="c1"># most_common(int) 按照元素出现的次数进行从高到低的排序,返回前int个元素的字典  </span>
<span class="n">m1</span> <span class="o">=</span> <span class="n">Counter</span><span class="p">(</span><span class="n">s</span><span class="p">)</span>  
<span class="nb">print</span><span class="p">(</span><span class="n">m1</span><span class="p">)</span>                 <span class="c1"># Counter({&#39;c&#39;: 4, &#39;b&#39;: 4, &#39;a&#39;: 3, &#39;d&#39;: 1})  </span>
<span class="nb">print</span><span class="p">(</span><span class="n">m1</span><span class="o">.</span><span class="n">most_common</span><span class="p">(</span><span class="mi">3</span><span class="p">))</span>  <span class="c1"># [(&#39;c&#39;, 4), (&#39;b&#39;, 4), (&#39;a&#39;, 3)]  </span>
</code></pre></td></tr></table>
</div>
</div><p><strong>2. elements</strong></p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span><span class="lnt">5
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-python" data-lang="python"><span class="c1"># elements 返回经过计数器Counter后的元素,返回的是一个迭代器  </span>
<span class="n">e1</span> <span class="o">=</span> <span class="n">Counter</span><span class="p">(</span><span class="n">s</span><span class="p">)</span>  
<span class="nb">print</span><span class="p">(</span><span class="s1">&#39;&#39;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="nb">sorted</span><span class="p">(</span><span class="n">e1</span><span class="o">.</span><span class="n">elements</span><span class="p">())))</span>  <span class="c1"># aaabbbbcccc  </span>
<span class="n">e2</span> <span class="o">=</span> <span class="n">Counter</span><span class="p">(</span><span class="n">d</span><span class="p">)</span>  
<span class="nb">print</span><span class="p">(</span><span class="nb">sorted</span><span class="p">(</span><span class="n">e2</span><span class="o">.</span><span class="n">elements</span><span class="p">()))</span>  <span class="c1"># [&#39;17&#39;, &#39;17&#39;, &#39;2&#39;, &#39;2&#39;, &#39;2&#39;, &#39;3&#39;, &#39;3&#39;] 字典返回value个key  </span>
</code></pre></td></tr></table>
</div>
</div><p><strong>3. update</strong></p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-python" data-lang="python"><span class="c1"># update 和set集合的update一样,对集合进行并集更新  </span>
<span class="n">u1</span> <span class="o">=</span> <span class="n">Counter</span><span class="p">(</span><span class="n">s</span><span class="p">)</span>  
<span class="n">u1</span><span class="o">.</span><span class="n">update</span><span class="p">(</span><span class="s1">&#39;123a&#39;</span><span class="p">)</span>  
<span class="nb">print</span><span class="p">(</span><span class="n">u1</span><span class="p">)</span>  <span class="c1"># Counter({&#39;a&#39;: 4, &#39;c&#39;: 4, &#39;b&#39;: 4, &#39;1&#39;: 1, &#39;3&#39;: 1, &#39;2&#39;: 1})  </span>
</code></pre></td></tr></table>
</div>
</div><p><strong>4. substract</strong></p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span><span class="lnt">5
</span><span class="lnt">6
</span><span class="lnt">7
</span><span class="lnt">8
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-python" data-lang="python"><span class="c1"># substract 和update类似，只是update是做加法，substract做减法,从另一个集合中减去本集合的元素，  </span>
<span class="n">sub1</span> <span class="o">=</span> <span class="s1">&#39;which&#39;</span>  
<span class="n">sub2</span> <span class="o">=</span> <span class="s1">&#39;whatw&#39;</span>  
<span class="n">subset</span> <span class="o">=</span> <span class="n">Counter</span><span class="p">(</span><span class="n">sub1</span><span class="p">)</span>  
<span class="nb">print</span><span class="p">(</span><span class="n">subset</span><span class="p">)</span>   <span class="c1"># Counter({&#39;h&#39;: 2, &#39;i&#39;: 1, &#39;c&#39;: 1, &#39;w&#39;: 1})  </span>
<span class="n">subset</span><span class="o">.</span><span class="n">subtract</span><span class="p">(</span><span class="n">Counter</span><span class="p">(</span><span class="n">sub2</span><span class="p">))</span>  
<span class="nb">print</span><span class="p">(</span><span class="n">subset</span><span class="p">)</span>   <span class="c1"># Counter({&#39;c&#39;: 1, &#39;i&#39;: 1, &#39;h&#39;: 1, &#39;a&#39;: -1, &#39;t&#39;: -1, &#39;w&#39;: -1})</span>
<span class="c1"># sub1中的h变为2，sub2中h为1,减完以后为1  </span>
</code></pre></td></tr></table>
</div>
</div><div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span><span class="lnt">5
</span><span class="lnt">6
</span><span class="lnt">7
</span><span class="lnt">8
</span><span class="lnt">9
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-fallback" data-lang="fallback">iteritems
与字典dict的items类似，返回由Counter生成的字典的所有item,
只是在Counter中此方法返回的是一个迭代器,而不是列表
iterkeys
与字典dict的keys方法类似，返回由Counter生成的字典的所有key,
只是在Counter中此方法返回的是一个迭代器,而不是列表
itervalues
与字典dict的values方法类似，返回由Counter生成的字典的所有value,
只是在Counter中此方法返回的是一个迭代器,而不是列表
</code></pre></td></tr></table>
</div>
</div><h4 id="2-默认字典defaultdict">2. 默认字典defaultdict</h4>
<p>默认字典,是字典的一个子类,继承有字典的方法和属性,默认字典在进行定义初始化的时候可以指定字典值得默认类型：</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span><span class="lnt">14
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-bash" data-lang="bash">&gt;&gt;&gt; rom collections import defaultdict
&gt;&gt;&gt; <span class="nv">dict1</span> <span class="o">=</span> defaultdict<span class="o">(</span>list<span class="o">)</span>   <span class="c1">#创建默认字典</span>
&gt;&gt;&gt; dict1
defaultdict<span class="o">(</span>&lt;class <span class="s1">&#39;list&#39;</span>&gt;, <span class="o">{})</span>
&gt;&gt;&gt; print<span class="o">(</span>type<span class="o">(</span>dict1<span class="o">))</span>
&lt;class <span class="s1">&#39;collections.defaultdict&#39;</span>&gt;
&gt;&gt;&gt; <span class="k">for</span> i in range<span class="o">(</span>20<span class="o">)</span>:  <span class="c1">#实例</span>
...   <span class="k">if</span> i%2 <span class="o">==</span> 0:
...     dict1<span class="o">[</span><span class="s1">&#39;k1&#39;</span><span class="o">]</span>.append<span class="o">(</span>i<span class="o">)</span>
...   <span class="k">else</span>:
...     dict1<span class="o">[</span><span class="s1">&#39;k2&#39;</span><span class="o">]</span>.append<span class="o">(</span>i<span class="o">)</span>
... 
&gt;&gt;&gt; dict1
defaultdict<span class="o">(</span>&lt;class <span class="s1">&#39;list&#39;</span>&gt;, <span class="o">{</span><span class="s1">&#39;k1&#39;</span>: <span class="o">[</span>0, 2, 4, 6, 8, 10, 12, 14, 16, 18<span class="o">]</span>, <span class="s1">&#39;k2&#39;</span>: <span class="o">[</span>1, 3, 5, 7, 9, 11, 13, 15, 17, 19<span class="o">]})</span>
</code></pre></td></tr></table>
</div>
</div><h4 id="3-可命名元组namedtuple">3. 可命名元组(namedtuple)</h4>
<p>根据namedtuple可以创建一个包含tuple所有功能以及其他功能的类型</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-bash" data-lang="bash">&gt;&gt;&gt; from collections import namedtuple
&gt;&gt;&gt; <span class="nv">tup1</span> <span class="o">=</span> namedtuple<span class="o">(</span><span class="s1">&#39;tup1&#39;</span>,<span class="o">[</span><span class="s1">&#39;x&#39;</span>,<span class="s1">&#39;y&#39;</span>,<span class="s1">&#39;z&#39;</span>,<span class="s1">&#39;u&#39;</span><span class="o">])</span>  <span class="c1">#创建可命名元组</span>
&gt;&gt;&gt; <span class="nv">obj</span> <span class="o">=</span> tup1<span class="o">(</span>111,22,333,4444<span class="o">)</span>  <span class="c1">#创建对象</span>
&gt;&gt;&gt; print<span class="o">(</span>obj.x<span class="o">)</span>   <span class="c1">#输出对象序列的值</span>
<span class="m">111</span>
&gt;&gt;&gt; print<span class="o">(</span>obj.y<span class="o">)</span>
<span class="m">22</span>
&gt;&gt;&gt; print<span class="o">(</span>obj.z<span class="o">)</span>
<span class="m">333</span>
&gt;&gt;&gt; print<span class="o">(</span>obj.u<span class="o">)</span>
<span class="m">4444</span>
</code></pre></td></tr></table>
</div>
</div><h4 id="4-有序字典ordereddict">4. 有序字典(OrderedDict)</h4>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-bash" data-lang="bash">&gt;&gt;&gt; from collections import OrderedDict
&gt;&gt;&gt; <span class="nv">dic1</span> <span class="o">=</span> OrderedDict<span class="o">({</span><span class="s1">&#39;k1&#39;</span>:<span class="s1">&#39;v1&#39;</span>,<span class="s1">&#39;k2&#39;</span>:<span class="s1">&#39;v2&#39;</span>,<span class="s1">&#39;k4&#39;</span>:<span class="s1">&#39;v4&#39;</span>,<span class="s1">&#39;k3&#39;</span>:<span class="s1">&#39;v3&#39;</span><span class="o">})</span>
&gt;&gt;&gt; dic1    <span class="c1">#创建有序列的字典</span>
OrderedDict<span class="o">([(</span><span class="s1">&#39;k4&#39;</span>, <span class="s1">&#39;v4&#39;</span><span class="o">)</span>, <span class="o">(</span><span class="s1">&#39;k1&#39;</span>, <span class="s1">&#39;v1&#39;</span><span class="o">)</span>, <span class="o">(</span><span class="s1">&#39;k2&#39;</span>, <span class="s1">&#39;v2&#39;</span><span class="o">)</span>, <span class="o">(</span><span class="s1">&#39;k3&#39;</span>, <span class="s1">&#39;v3&#39;</span><span class="o">)])</span>
&gt;&gt;&gt; print<span class="o">(</span>type<span class="o">(</span>dic1<span class="o">))</span>
&lt;class <span class="s1">&#39;collections.OrderedDict&#39;</span>&gt;
&gt;&gt;&gt; dic1.popitem<span class="o">()</span>    <span class="c1">#默认提取最后要给key</span>
<span class="o">(</span><span class="s1">&#39;k3&#39;</span>, <span class="s1">&#39;v3&#39;</span><span class="o">)</span>
&gt;&gt;&gt; dic1
OrderedDict<span class="o">([(</span><span class="s1">&#39;k4&#39;</span>, <span class="s1">&#39;v4&#39;</span><span class="o">)</span>, <span class="o">(</span><span class="s1">&#39;k1&#39;</span>, <span class="s1">&#39;v1&#39;</span><span class="o">)</span>, <span class="o">(</span><span class="s1">&#39;k2&#39;</span>, <span class="s1">&#39;v2&#39;</span><span class="o">)])</span>
&gt;&gt;&gt; dic1.move_to_end<span class="o">(</span><span class="s1">&#39;k4&#39;</span><span class="o">)</span>   <span class="c1">#将指定key移到最后</span>
&gt;&gt;&gt; dic1
OrderedDict<span class="o">([(</span><span class="s1">&#39;k1&#39;</span>, <span class="s1">&#39;v1&#39;</span><span class="o">)</span>, <span class="o">(</span><span class="s1">&#39;k2&#39;</span>, <span class="s1">&#39;v2&#39;</span><span class="o">)</span>, <span class="o">(</span><span class="s1">&#39;k4&#39;</span>, <span class="s1">&#39;v4&#39;</span><span class="o">)])</span>
</code></pre></td></tr></table>
</div>
</div><h4 id="kennethreitz">kennethreitz</h4>
<ul>
<li>requests 请求库</li>
<li>requests-html 请求+解析html（集成PyQuery,lxml）</li>
<li>tablib 格式不可知的表格数据集库</li>
<li>maya 日期时间</li>
</ul>

    </div>

    <div class="post-copyright">
  <p class="copyright-item">
    <span class="item-title">文章作者</span>
    <span class="item-content">even</span>
  </p>
  <p class="copyright-item">
    <span class="item-title">上次更新</span>
    <span class="item-content">
        2018-12-10
        
    </span>
  </p>
  
  
</div>
<footer class="post-footer">
      <div class="post-tags">
          <a href="/tags/python/">Python</a>
          </div>
      <nav class="post-nav">
        <a class="prev" href="/post/python/python3%E5%A4%9A%E8%BF%9B%E7%A8%8B%E5%92%8C%E5%A4%9A%E7%BA%BF%E7%A8%8B/">
            <i class="iconfont icon-left"></i>
            <span class="prev-text nav-default">Python3 多进程和多线程</span>
            <span class="prev-text nav-mobile">上一篇</span>
          </a>
        <a class="next" href="/post/python/pip%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0/">
            <span class="next-text nav-default">pip学习笔记</span>
            <span class="next-text nav-mobile">下一篇</span>
            <i class="iconfont icon-right"></i>
          </a>
      </nav>
    </footer>
  </article>
        </div>
        

  

  

      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="social-links">
      <a href="mailto:your@email.com" class="iconfont icon-email" title="email"></a>
      <a href="http://localhost:1313" class="iconfont icon-stack-overflow" title="stack-overflow"></a>
      <a href="http://localhost:1313" class="iconfont icon-twitter" title="twitter"></a>
      <a href="http://localhost:1313" class="iconfont icon-facebook" title="facebook"></a>
      <a href="http://localhost:1313" class="iconfont icon-linkedin" title="linkedin"></a>
      <a href="http://localhost:1313" class="iconfont icon-google" title="google"></a>
      <a href="http://localhost:1313" class="iconfont icon-github" title="github"></a>
      <a href="http://localhost:1313" class="iconfont icon-weibo" title="weibo"></a>
      <a href="http://localhost:1313" class="iconfont icon-zhihu" title="zhihu"></a>
      <a href="http://localhost:1313" class="iconfont icon-douban" title="douban"></a>
      <a href="http://localhost:1313" class="iconfont icon-pocket" title="pocket"></a>
      <a href="http://localhost:1313" class="iconfont icon-tumblr" title="tumblr"></a>
      <a href="http://localhost:1313" class="iconfont icon-instagram" title="instagram"></a>
      <a href="http://localhost:1313" class="iconfont icon-gitlab" title="gitlab"></a>
      <a href="http://localhost:1313" class="iconfont icon-bilibili" title="bilibili"></a>
  <a href="https://zhouxiaoxin.gitee.io/index.xml" type="application/rss+xml" class="iconfont icon-rss" title="rss"></a>
</div>

<div class="copyright">
  <span class="power-by">
    由 <a class="hexo-link" href="https://gohugo.io">Hugo</a> 强力驱动
  </span>
  <span class="division">|</span>
  <span class="theme-info">
    主题 - 
    <a class="theme-link" href="https://github.com/olOwOlo/hugo-theme-even">Even</a>
  </span>

  <div class="busuanzi-footer">
    <span id="busuanzi_container_site_pv"> 本站总访问量 <span id="busuanzi_value_site_pv"><img src="/img/spinner.svg" alt="spinner.svg"/></span> 次 </span>
      <span class="division">|</span>
    <span id="busuanzi_container_site_uv"> 本站总访客数 <span id="busuanzi_value_site_uv"><img src="/img/spinner.svg" alt="spinner.svg"/></span> 人 </span>
  </div>

  <span class="copyright-year">
    &copy; 
    2018 - 
    2022
    <span class="heart">
      <i class="iconfont icon-heart"></i>
    </span>
    <span class="author">even</span>
  </span>
</div>
    </footer>

    <div class="back-to-top" id="back-to-top">
      <i class="iconfont icon-up"></i>
    </div>
  </div>
  
  <script src="https://cdn.jsdelivr.net/npm/jquery@3.2.1/dist/jquery.min.js" integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4=" crossorigin="anonymous"></script>
  <script src="https://cdn.jsdelivr.net/npm/slideout@1.0.1/dist/slideout.min.js" integrity="sha256-t+zJ/g8/KXIJMjSVQdnibt4dlaDxc9zXr/9oNPeWqdg=" crossorigin="anonymous"></script>
  <script src="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@3.1.20/dist/jquery.fancybox.min.js" integrity="sha256-XVLffZaxoWfGUEbdzuLi7pwaUJv1cecsQJQqGLe7axY=" crossorigin="anonymous"></script>



<script type="text/javascript" src="/js/main.min.2517c0eb67172a0bae917de4af59b10ca2531411a009d4c0b82f5685259e5771.js"></script>








</body>
</html>
